从包含str和int python的列表中查找最大int值 [英] Find maximum int value from list which contain both str and int python
本文介绍了从包含str和int python的列表中查找最大int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要从组合列表中找到max
,如下所示:
Looking to find max
from the combine list as follows:
['filename1', 1696, 'filename2', 5809,....]
我尝试了以下操作:
max(['filename1', 1696, 'filename2', 5809,....])
那使我回到TypeError: '>' not supported between instances of 'int' and 'str'
.任何建议都会有所帮助.我想要的是从上述列表中找到max
整数值.
that return me TypeError: '>' not supported between instances of 'int' and 'str'
. Any suggestion would help. What I want is to find the max
integer value from the list above mentioned.
推荐答案
使用list comprehension
和isinstance
提取int
,然后使用max
.
Use list comprehension
with isinstance
to extract the int
and then use max
.
例如:
f = ['filename1', 1696, 'filename2', 5809]
print(max([i for i in f if isinstance(i, int)]))
#or generator
print(max((i for i in f if isinstance(i, int)))) #Better Option
输出:
5809
5809
这篇关于从包含str和int python的列表中查找最大int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文