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

时间:2015-10-10 12:09:18

标签: python list duplicates

假设我的清单如下:

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'

我将如何做到这一点?

1 个答案:

答案 0 :(得分:0)

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