python - Combining lists of tuples based on a common tuple element -
consider 2 lists of tuples:
data1 = [([x1], 'a'), ([x2], 'b'), ([x3], 'c')] data2 = [([y1], 'a'), ([y2], 'b'), ([y3], 'c')]
where len(data1) == len(data2)
each tuple contains 2 elements:
- list of strings (i.e
[x1]
) - a common element
data1
,data2
: strings'a'
,'b'
, , on.
i combine them following:
[('a', [x1], [y1]), ('b', [x2], [y2]),...]
does know how can this?
you can use zip
function , list comprehension:
[(s1,l1,l2) (l1,s1),(l2,s2) in zip(data1,data2)]
Comments
Post a Comment