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

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -