简体   繁体   中英

how to find largest number in list

So i'm trying to find the third largest number/string in a list. This is the code that i have so far

def big(inputString):
    big1 = inputString[0]
    big2 = inputString[0]
    big3 = inputString[0]
    for char in inputString:
        if char > big1:
            big1, big2,big3 = char,big1,big2
        elif big1 > char > big2 > big3:
            big3 = char
    print('largest',big1,'second largest',big2,third largest,big3)

when the user inputs a list of string the out put should look like this:

big('abxztu')
largest z second largest x third largest u

but the output that i get is

largest z second largest x third largest b

Can anyone tell me where the mistake is in my code

You aren't handling the case where big2 > char > big3.

Also instead of this manual brute force approach, you can just sort the string and then print the characters in any order.

s = sorted('abxztu')
print s # ['a', 'b', 't', 'u', 'x', 'z']

Now print them from the tail or if you want reverse s and print them from the head.

Even a better answer suggested by @Chris_Rands is to use heapq.nlargest.

print heapq.nlargest(3, 'abxztu') #['z', 'x', 'u']

You could simply do this:

def big(inputString):
    l = sorted(list(inputString)) #Turning the input into a list and sorting it
    l = l[::-1] #Reversing the list
    print(l[0],l[1],l[2]) #Printing out the first three element of the list

This is a example of a test run:

>>> big("abc")
c b a

Edit 1

Another example of a test run:

>>> big("helloworld")
w r o

You can use the built-in sorted to sort a list. sorted accepts a reverse parameter (a boolean) so you can sort the list descending.

You can even pass a string to sorted and it will sort the individual characters into a list.

To get the third, or second, or nth biggest item in the list, just sort the list in descending order then call that item, eg obj[0] for the largest item, obj[1] for the second-largest, etc.

Let's put it together:

""" Returns nth largest object from string or list """
def nth_largest(obj, n=1):
    obj = sorted(obj, reverse=True)
    return obj[n-1]

>>> nth_largest('abxztu', 3)
'u'

One condition is missing when char is less than big1 and greater than big2

Code :

def big(inputString):
    big1 = inputString[0]
    big2 = inputString[0]
    big3 = inputString[0]

    for char in inputString:
        if char > big1:
            big1, big2, big3 = char, big1, big2
        elif big1 > char > big2 > big3:
            big3 = char
        elif big3 < char < big1 > big2:
            big3 = char

    print('largest',big1,'second largest',big2,'third largest',big3)

For top 3 biggest:

import string
def bigger(s):
    l = [(i,string.ascii_letters.index(i)) for i in s]
    max1=max(l,key=lambda x: x[1])
    l.remove(max1)
    max2=max(l,key=lambda x: x[1])
    l.remove(max2)
    max3=max(l,key=lambda x: x[1])
    return '\n'.join([max1[0],max2[0],max3[0]])

print(bigger('zsabx'))

Output:

z
x
s

For biggest:

import string
def bigger(s):
    l = [(i,string.ascii_letters.index(i)) for i in s]
    max1=max(l,key=lambda x: x[1])
    return max1[0]

print(bigger('zsabx'))

Output:

z

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM