Python元组列表将第二个元素与唯一的第一个元素合并

问题描述 投票:5回答:5

给出一个像这样的元组列表:

a = [ ( "x", 1, ), ( "x", 2, ), ( "y", 1, ), ( "y", 3, ), ( "y", 4, ) ]

过滤唯一第一个元素并合并第二个元素的最简单方法是什么。像这样的输出是期望的。

b = [ ( "x", 1, 2 ), ( "y", 1, 3, 4 ) ]

谢谢,

python list merge tuples unique
5个回答
2
投票

你可以使用defaultdict

>>> from collections import defaultdict
>>> d = defaultdict(tuple)
>>> a = [('x', 1), ('x', 2), ('y', 1), ('y', 3), ('y', 4)]
>>> for tup in a:
...     d[tup[0]] += (tup[1],)
...
>>> [tuple(x for y in i for x in y) for i in d.items()]
[('y', 1, 3, 4), ('x', 1, 2)]

5
投票
>>> a = [("x", 1,), ("x", 2,), ("y", 1,), ("y", 3,), ("y", 4,)]
>>> d = {}
>>> for k, v in a:
...     d.setdefault(k, [k]).append(v)
>>> b = map(tuple, d.values())
>>> b
[('y', 1, 3, 4), ('x', 1, 2)]

1
投票

这就是我想出的:

[tuple(list(el) + [q[1] for q in a if q[0]==el]) for el in set([q[0] for q in a])]

0
投票

除了之前的答案,另一个单行:

>>> a = [ ( "x", 1, ), ( "x", 2, ), ( "y", 1, ), ( "y", 3, ), ( "y", 4, ) ]
>>> from itertools import groupby
>>> [(key,) + tuple(elem for _, elem in group) for key, group in groupby(a, lambda pair: pair[0])]
[('x', 1, 2), ('y', 1, 3, 4)]

0
投票

一种方法是使用itertools.groupbyitertools.chainoperator.itemgetter的列表理解表达式:

>>> from itertools import groupby, chain
>>> from operator import itemgetter

>>> my_list = [ ( "x", 1, ), ( "x", 2, ), ( "y", 1, ), ( "y", 3, ), ( "y", 4, ) ]

>>> [set(chain(*i)) for _, i in groupby(sorted(my_list), key=itemgetter(0))]
[set(['x', 2, 1]), set(['y', 1, 3, 4])]

注意:set本质上是无序的,因此它们不会保留元素的位置。如果位置很重要,请不要使用set

© www.soinside.com 2019 - 2024. All rights reserved.