简体   繁体   中英

Merge two multidimensional lists into one list in Python

I have two lists:

a_list = 
[['2017-06-03 23:01:49', 0], ['2017-06-03 23:02:49', 712.32], ['2017-06-03 23:03:49', 501.21].......]

b_list = 
[['2017-06-03 23:01:49', 100.01], ['2017-06-03 23:02:49', 50.01], ['2017-06-03 23:03:49', 521.79].......]

I need to merge a_list with b_list so it becomes:

combined_list =
[['2017-06-03 23:01:49', 0, 100,01], ['2017-06-03 23:02:49', 712.32, 50.01], ['2017-06-03 23:03:49', 501.21, 521.79].......]

How would I achieve this?

a_list = [['2017-06-03 23:01:49', 0], ['2017-06-03 23:02:49', 712.32], ['2017-06-03 23:03:49', 501.21]]
b_list = [['2017-06-03 23:01:49', 100.01], ['2017-06-03 23:02:49', 50.01], ['2017-06-03 23:03:49', 521.79]]

Assuming a_list and b_list have the same length, and assuming the first sub-item of each item in both lists is always the same, as is true per your example, the solution is a one-liner.

>>> [a + [b[1]] for (a, b) in zip(a_list, b_list)]
[[['2017-06-03 23:01:49', 0, 100.01]], [['2017-06-03 23:02:49', 712.32, 50.01]], [['2017-06-03 23:03:49', 501.21, 521.79]]]

You can use zip() and unpack your data all along with a list comprehension like this example:

a = [['2017-06-03 23:01:49', 0],
 ['2017-06-03 23:02:49', 712.32],
 ['2017-06-03 23:03:49', 501.21]]

b = [['2017-06-03 23:01:49', 100.01],
 ['2017-06-03 23:02:49', 50.01],
 ['2017-06-03 23:03:49', 521.79]]

final = [[k,v,j] for (k,v),(_,j) in zip(a, b)]
print(final)

Output:

[['2017-06-03 23:01:49', 0, 100.01],
 ['2017-06-03 23:02:49', 712.32, 50.01],
 ['2017-06-03 23:03:49', 501.21, 521.79]]

Try with this, merge two list, then use defaultdict to generate new dictioanry, find same key, append the value to super_dict, then convert the dictionary to list format:

import collections
a_list = [['2017-06-03 23:01:49', 0], ['2017-06-03 23:02:49', 712.32], ['2017-06-03 23:03:49', 501.21]]
b_list = [['2017-06-03 23:01:49', 100.01], ['2017-06-03 23:02:49', 50.01], ['2017-06-03 23:03:49', 521.79]]
super_dict = collections.defaultdict(list)

for e in a_list+b_list:
    super_dict[e[0]].append(e[1])

dictlist=list()
for key, value in super_dict.iteritems():
    dictlist.append([key]+value)
dictlist

Output:

[['2017-06-03 23:02:49', 712.32, 50.01],
 ['2017-06-03 23:03:49', 501.21, 521.79],
 ['2017-06-03 23:01:49', 0, 100.01]]

The code below should work. The idea is that you want to take a list, a , and create a dictionary with the key set to first element in each sublist of a . The value associated with that list is a list containing the second element in that sublist of a . Then we loop through the second list b and do the same. Finally, we iterate over the dictionary that we created, D , and turn that into a list.

a = [[1,2], [3,4], [5,6]]
b = [[1,20],[3,40],[5, 50]]
D = {}
for i in a:
    D[i[0]] = [i[1]]

for i in b:
    D[i[0]].append(i[1])

finalList = []
for d in D:
    finalList.append([d, D[d][0], D[d][1]])

print(finalList)
>>> [[1, 2, 10], [3, 4, 40], [5, 6, 50]]

Note, this program assumes that the two corresponding sublists are guaranteed to have equal first elements. If this is not the case or the two lists a and b are not equal, you'll need an if statement on line 8 to check if the key value i[0] is in the list.

Just throwing my attempt in the mix:

list1 = [["a","b"],["c","d"]]
list2 = [["a","1"],["c","2"]]
list3 = [(sub + [list2[i][-1]]) for i, sub in enumerate(list1)]
#[['a', 'b', '1'], ['c', 'd', '2']]

This is pretty close to @ABB's though if zip uses enumerate.

a_list = [['2017-06-03 23:01:49', 0], ['2017-06-03 23:02:49', 712.32], ['2017-06-03 23:03:49', 501.21]]

b_list = [['2017-06-03 23:01:49', 100.01], ['2017-06-03 23:02:49', 50.01], ['2017-06-03 23:03:49', 521.79]]

combined_list = []
for index in range(3):
    x = a_list[index] + b_list[index]
    x.pop(2)
    combined_list.append(x)
print(combined_list)    

If you are looking for a one liner, then I thought I would throw it a try,

[set(item[0]).union(item[1]) for item in list(zip(a_list, b_list))]
>>>[{0, 100.01000000000001, '2017-06-03 23:01:49'}, {'2017-06-03 23:02:49', 50.009999999999998, 712.32000000000005}, {521.78999999999996, 501.20999999999998, '2017-06-03 23:03:49'}]
>>>

The result would be a set though. There won't be any duplication of elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM