问题
Given a list of tuples like so:
a = [ ( "x", 1, ), ( "x", 2, ), ( "y", 1, ), ( "y", 3, ), ( "y", 4, ) ]
What would be the easiest way to filter for unique first element and merge the second element. An output like so would be desired.
b = [ ( "x", 1, 2 ), ( "y", 1, 3, 4 ) ]
Thanks,
回答1:
You can use a 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)]
回答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)]
回答3:
This is what I came up with:
[tuple(list(el) + [q[1] for q in a if q[0]==el]) for el in set([q[0] for q in a])]
回答4:
In addition to previous answers, another one-liner:
>>> 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)]
回答5:
One way is by using list comprehension expression with itertools.groupby , itertools.chain and operator.itemgetter as:
>>> 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])]
Note: set
are unordered in nature, so they won't preserve the position of elements. Do not use set
if the position matters.
来源:https://stackoverflow.com/questions/18909280/python-list-of-tuples-merge-2nd-element-with-unique-first-element