简体   繁体   English

从列表中找到最大的数字字符串

[英]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. 使用maxkey参数是另一种明智的方法。

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. 请注意,您不必事先对list1进行排序,在最坏的情况下,找到最大值永远不会比O(N)复杂。

The reason for your current output is because you call list.sort on a list of strings. 当前输出的原因是因为您在字符串列表上调用list.sort sort will compare strings lexicographically by default, and '9' > '4'. 默认情况下, sort将按字典顺序比较字符串,并且'9'>'4'。

Convert strings to int and find the max : strings转换为int并找到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 现在,您正在比较一个字符串列表,该排序按字典顺序进行,如果以这种方式排序,则'99999'是按字典顺序排列的最大值

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) 排序+最大发现将为O(n*logn)而仅找到最大将为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 . 应该使用mapmax 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. 您必须先将项目从str转换为int I did that here with map(int, list1) which applies int() to every item in list1 . 我在这里使用map(int, list1)此操作,该方法将int()应用于list1每个项目。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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