Merging 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 combine a_listwith b_listso that 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 can i achieve this?

+4
source share
7 answers
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 that a_listthey b_listhave the same length, and if the first sub-item of each element in both lists is always the same as in your example, the solution is single-line.

>>> [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]]]
+2
source

You can use zip()and unzip your data along with 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)

Conclusion:

[['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]]
+2
source

, , defaultdict dictioanry, , super_dict, :

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

:

[['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]]
+1

. , a , a. , , , a. b . , , , D, .

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

: , . , a b , if line 8, , i[0] .

0

Just throw my attempt at 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, though if zip uses an enum.

0
source
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)    
0
source

If you are looking for one airliner, then I thought that I would give 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 is a set. There will be no duplication of elements.

0
source

Source: https://habr.com/ru/post/1678835/


All Articles