在Python中的元组列表中查找重复值

时间:2016-07-27 17:47:12

标签: python list tuples

如何在以下元组列表中找到重复值?

[(1622, 4081), (1622, 4082), (1624, 4083), (1626, 4085), (1650, 4086), (1650, 4090)]

我希望得到一个列表:

[4081, 4082, 4086, 4090]

我已尝试使用itemgetter然后按选项分组,但无效。

怎么能这样做?

4 个答案:

答案 0 :(得分:3)

使用有序字典作为其键和第二项列表作为值(对于使用dict.setdefalt()创建的重复项)然后选取长度大于1的那些:

>>> from itertools import chain
>>> from collections import OrderedDict
>>> d = OrderedDict()
>>> for i, j in lst:
...     d.setdefault(i,[]).append(j)
... 
>>> 
>>> list(chain.from_iterable([j for i, j in d.items() if len(j)>1]))
[4081, 4082, 4086, 4090]

答案 1 :(得分:1)

作为替代方案,如果您想使用groupby,可以使用以下方法:

In [1]: from itertools import groupby

In [2]: ts = [(1622, 4081), (1622, 4082), (1624, 4083), (1626, 4085), (1650, 4086), (1650, 4090)]

In [3]: dups = []

In [4]: for _, g in groupby(ts, lambda x: x[0]):
   ...:     grouped = list(g)
   ...:     if len(grouped) > 1:
   ...:         dups.extend([dup[1] for dup in grouped])
   ...:         

In [5]: print(dups)
[4081, 4082, 4086, 4090]

使用groupby从元组的第一个元素进行分组,并将重复的值添加到元组的列表中。

答案 2 :(得分:1)

另一种方法(没有任何进口):

In [896]: lot = [(1622, 4081), (1622, 4082), (1624, 4083), (1626, 4085), (1650, 4086), (1650, 4090)]

In [897]: d = dict()

In [898]: for key, value in lot:
     ...:     d[key] = d.get(key, []) + [value]
     ...: 
     ...: 

In [899]: d
Out[899]: {1622: [4081, 4082], 1624: [4083], 1626: [4085], 1650: [4086, 4090]}

In [900]: [d[key] for key in d if len(d[key]) > 1]
Out[900]: [[4086, 4090], [4081, 4082]]

In [901]: sorted([num for num in lst for lst in [d[key] for key in d if len(d[key]) > 1]])
Out[901]: [4081, 4081, 4082, 4082]

答案 3 :(得分:0)

没有测试过这个....(编辑:是的,它有效)

l = [(1622, 4081), (1622, 4082), (1624, 4083), (1626, 4085), (1650, 4086), (1650, 4090)]

dup = []

for i, t1 in enumerate(l):
    for t2 in l[i+1:]:
        if t1[0]==t2[0]:
            dup.extend([t1[1], t2[1]])
print dup