tup中的Python重复(秒)值

2024-09-25 10:23:06 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我的清单如下:

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'

我该怎么做呢


Tags: newstringnotsomedootherlist1stringa
1条回答
网友
1楼 · 发布于 2024-09-25 10:23:06

您可以使用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')]
>>> 

相关问题 更多 >