在列表列表中搜索最多一列 [英] search the biggest number of one column in a list of list
本文介绍了在列表列表中搜索最多一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表list = [(1,2,5), (2,8,7),(3,6,9)]
,我想找到第三列的最大数目,所以我尝试了:
I have an list of list, list = [(1,2,5), (2,8,7),(3,6,9)]
, and I want to find the biggest number of the third column, so I tried:
zipped = zip(*list)
print max(zipped[2])
但是它没有显示最大的数字.有人知道原因和解决方案吗?
But it does not show the biggest number. Anyone know why and a solution?
推荐答案
适用于所有较新的Python:
Works on all newer Pythons:
>>> li = [(1,2,5), (2,8,7),(3,6,9)]
>>> max(l[2] for l in li)
9
如果您有文字:
>>> li = [('1','2','5'), ('2','8','7'),('3','6','9')]
>>> max(int(l[2]) for l in li)
9
并且即使源是迭代器/生成器也可以工作.这是在Py3.3上,其中zip返回一个迭代器:
And works even if the source is an iterator / generator. This is on Py3.3 where zip returns an iterator:
>>> gli=(e for e in li)
>>> max(int(l[2]) for l in gli)
9
>>> max(int(l[2]) for l in zip(*li))
9
这篇关于在列表列表中搜索最多一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文