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:

  1. list of strings (i.e [x1])
  2. 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

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -