繁体   English   中英

Python - 元组中的重复(第二个)值

[英]Python - Repeated (second)) value in tuple

假设我的清单如下:

list1 = [('do not care1', 'some string'),
    ('do not care1', 'some new string'),
    ('do not care2', 'some string'),
    ('do not care3', 'some other stringA')
    ('do not care4', 'some other stringA')
    ('do not care10', 'some other stringB')
    ('do not care54', 'some string')

仅当第二个值重复超过 2 次时,我才需要整个条目。

在上面的例子中,我希望看到这样的输出

'do not care1', 'some string'
'do not care2', 'some string'
'do not care54', 'some string'

我该怎么做呢?

您可以使用collections.Counter和列表理解:

>>> form collections import Counter
>>> [i for i in list1 if i[1] in [item for item,val in Counter(zip(*list1)[1]).items() if val>2]]
[('do not care1', 'some string'), ('do not care2', 'some string'), ('do not care54', 'some string')]
>>> 

暂无
暂无

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

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