假设我有下面的元组
dummy = [("text", 10), ("This is the Sentence", 20),
("that I Want", 20), ("to Get", 20),
("text", 8), ("text", 6)]
我想得到“This is the Sentence that I Want to Get
”,然后忽略其余部分。特别是文本总是具有最大值(在本例中为20),并且它们彼此相邻。基本上,它将只收集彼此相邻的具有最大值的元组
在下面的代码中,我只收集第一个max元组,但它忽略了其余的
from operator import itemgetter
max(dummy, key=itemgetter(1))
如何使其获得所有其他最大值
以下是我的方法:
通过使用namedtuples作为虚拟项,可以进一步改进代码
为什么不: 使用dict值获取max键并通过它实现过滤器
我的结果是:
像这样的
相关问题 更多 >
编程相关推荐