apol96
apol96

Reputation: 210

Combining nested lists according to first element

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!

Upvotes: 4

Views: 596

Answers (2)

algrebe
algrebe

Reputation: 1651

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)]

Upvotes: 2

Daniel Lenz
Daniel Lenz

Reputation: 3867

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]}
)

Upvotes: 3

Related Questions