简体   繁体   中英

Find the largest numeric string from a list

I have one list. I want to find max number but I got the wrong max number I try below code

list1=['400160', '400161', '400162', '400163', '400164', '400165', '400166', '400167', '400168', '400169', '400170', '400171', '400172', '400173', '400174', '400175', '400176', '400177','99990', '99991', '99992', '99993', '99994', '99995', '99996', '99997', '99998', '99999']
list1.sort() 

#printing the last element 
print("Largest element is:", list1[-1])
#print max(list1)

my output as below:-

print("Largest element is:", list1[-1])
('Largest element is:', '99999') 

but its wrong. how to find max number

Using the key argument of max is another sensible approach.

max(list1, key=int)
# '400177'

Note that you DO NOT have to sort list1 beforehand, finding the max should never be worse than O(N) in complexity in the worst case.

The reason for your current output is because you call list.sort on a list of strings. sort will compare strings lexicographically by default, and '9' > '4'.

Convert strings to int and find the max :

max(map(int, list1))

400177

Right now you are comparing a list of strings, and the sort happens lexicographically, and if you sort that way, '99999' is the maximum lexicographically

Hence you need to convert the list of strings to a list of numbers first, then use max builtin to find the maximum.

Sorting the list to find the maximum is perhaps an overkill.
Sorting + maximum finding will be O(n*logn) whereas just finding the maximum is O(n)

In [40]: list1=['400160', '400161', '400162', '400163', '400164', '400165', '400166', '400167', '400168', '400169', '400170', '400171', '400172', '400173', '400174', '400175', '400
    ...: 176', '400177','99990', '99991', '99992', '99993', '99994', '99995', '99996', '99997', '99998', '99999']                                                                   

In [41]: list1 = list(map(int,list1))                                                                                                                                               

In [43]: max(list1)                                                                                                                                                                 
Out[43]: 400177

Should use map and max . Like this:

list1=['400160', '400161', '400162', '400163', '400164', '400165', '400166', '400167', '400168', '400169', '400170', '400171', '400172', '400173', '400174', '400175', '400176', '400177','99990', '99991', '99992', '99993', '99994', '99995', '99996', '99997', '99998', '99999']
print(max(map(int, list1)))

Output:

>>>python3 test.py 
400177

This should do it:

max(map(int, list1))

you have to convert your items from str to int first. I did that here with map(int, list1) which applies int() to every item in list1 .

打印“最大的元素是:”,最大值(map(int,list1))

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