简体   繁体   English

从元组列表中查找最大值的所有出现?

[英]Find all occurences of maximum value from a list of tuple?

I have a list of tuples such as:我有一个元组列表,例如:

list1=[(1,1),(2,1),(3,1),(4,0),(5,0)]

I have found the maximum element using:我使用以下方法找到了最大元素:

max_value = max(list1, key=itemgetter(1))

This outputs: (1, 1)这输出: (1, 1)

I want some thing like: [(1,1),(2,1),(3,1)]我想要一些类似的东西: [(1,1),(2,1),(3,1)]

From Docs of max() :来自max()文档:

If multiple items are maximal, the function returns the first one encountered.如果多个项目是最大的,则该函数返回遇到的第一个项目。 This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc, reverse=True)[0] and heapq.nlargest(1, iterable, key=keyfunc).这与其他保持排序稳定性的工具一致,例如 sorted(iterable, key=keyfunc, reverse=True)[0] 和 heapq.nlargest(1, iterable, key=keyfunc)。

You could select all those values that match the max_value using list comprehension您可以使用列表理解选择与 max_value 匹配的所有值

max_value = max(list1, key=itemgetter(1))[1]
ans = [y for y in list1 if y[1] == max_value]
[(1, 1), (2, 1), (3, 1)]

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

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