4

So I have three lists:

lst1 = [('test1', 0.2), ('test7', 0.2)]
lst2 = [('test1', 5.2), ('test2', 11.1), ('test7', 0.2)]
lst3 = [('test1', 19.2), ('test2', 12.1), ('test7', 19.2), ('test9', 15.1)]

What I want to do is to go through the lists and create the following tuple:

[(test1, 0.2, 5.2, 19.2), (test2, 0.0, 11.1, 12.1), (test7, 0.2, 0.2, 19.2), (test9, 0.0, 0.0, 15.1)]

I have tried to solve this through multiple methods but no luck, any help is welcomed!

apol96
  • 200
  • 12
  • It would be helpful to also include the code that you've written till now. Also, your problem is similar to [Merge multiple dictionaries](https://stackoverflow.com/questions/5946236/how-to-merge-multiple-dicts-with-same-key) and [Merge python dictionaries](https://stackoverflow.com/questions/2365921/merging-python-dictionaries) except instead of key and value in dictionaries, you have a list of key value pairs. – algrebe Nov 29 '20 at 19:05

2 Answers2

3

If you really want to use test1 etc. as unique keys, you might be better off using a dictionary.

I'd recommend the following: Use itertools.chain to combine the lists that you want to iterate over, and use a default dictionary that you simply append items to.

import itertools as it
from collections import defaultdict

lst1 = [('test1', 0.2), ('test7', 0.2)]
lst2 = [('test1', 5.2), ('test2', 11.1), ('test7', 0.2)]
lst3 = [('test1', 19.2), ('test2', 12.1), ('test7', 19.2), ('test9', 15.1)]


mydict = defaultdict(list)

for key, value in it.chain(lst1, lst2, lst3):
  mydict[key].append(value)

print(mydict)

> defaultdict(
        <class 'list'>,
        {'test1': [0.2, 5.2, 19.2],
        'test7': [0.2, 0.2, 19.2],
        'test2': [11.1, 12.1],
        'test9': [15.1]}
)
Daniel Lenz
  • 3,334
  • 17
  • 36
  • This is not the output I want really, how can I make the output `{'test1': [0.2, 5.2, 19.2], 'test7': [0.2, 0.2, 19.2], 'test2': [0.0, 11.1, 12.1], 'test9': [0.0, 0.0, 15.1]}` – apol96 Nov 29 '20 at 21:27
  • How do you decide which lists get an extra `0.0` as first element and which don't? – Daniel Lenz Nov 30 '20 at 09:15
2
lst1 = [('test1', 0.2), ('test7', 0.2)]
lst2 = [('test1', 5.2), ('test2', 11.1), ('test7', 0.2)]
lst3 = [('test1', 19.2), ('test2', 12.1), ('test7', 19.2), ('test9', 15.1)]
# create a list containing all these lists
lst_of_lists = [lst1, lst2, lst3]
# Create a dictionary where keys are the test1, test2, test3...
# and the values are [0.0, 0.0, ....] as many as the number of lists
output_dict = {
    name:[0.0]*len(lst_of_lists) for lst in lst_of_lists
    for (name, value) in lst
}

# Now, 'test1' in lst1 has to modify output_dict['test1'][0]
#      'test1' in lst2 modifies output_dict['test1'][1]
# and so on...
for idx, lst in enumerate(lst_of_lists):
    for key, value in lst:
        output_dict[key][idx] = value

# this gives you a dictionary
print("output_dict: ", output_dict)

# you can convert it back to a list with .items(), and since you want it sorted
print("dict_as_list: ", sorted(output_dict.items()))

# since you want the keys and values together in the same tuple
output_list = [tuple([key] + val_list) for key, val_list in output_dict.items()]
# and sort that
output_list = sorted(output_list)
print("output_list: ", output_list)

Output:

output_dict:  {'test1': [0.2, 5.2, 19.2], 'test7': [0.2, 0.2, 19.2], 'test2': [0.0, 11.1, 12.1], 'test9': [0.0, 0.0, 15.1]}
dict_as_list:  [('test1', [0.2, 5.2, 19.2]), ('test2', [0.0, 11.1, 12.1]), ('test7', [0.2, 0.2, 19.2]), ('test9', [0.0, 0.0, 15.1])]
output_list:  [('test1', 0.2, 5.2, 19.2), ('test2', 0.0, 11.1, 12.1), ('test7', 0.2, 0.2, 19.2), ('test9', 0.0, 0.0, 15.1)]
algrebe
  • 1,621
  • 11
  • 17