给出一个像这样的元组列表:
a = [ ( "x", 1, ), ( "x", 2, ), ( "y", 1, ), ( "y", 3, ), ( "y", 4, ) ]
过滤唯一第一个元素并合并第二个元素的最简单方法是什么。像这样的输出是期望的。
b = [ ( "x", 1, 2 ), ( "y", 1, 3, 4 ) ]
谢谢,
你可以使用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)]
>>> 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)]
这就是我想出的:
[tuple(list(el) + [q[1] for q in a if q[0]==el]) for el in set([q[0] for q in a])]
除了之前的答案,另一个单行:
>>> 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)]
一种方法是使用itertools.groupby
,itertools.chain
和operator.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
。