Home > Net >  Merging multiple lists into one in Python
Merging multiple lists into one in Python

Time:09-13

I have a list X. I want to append elements of X[0][0],X[1][0],X[2][0], X[1][0],X[1][1],X[2][1] and so on. I present the current and expected outputs.

X=[[[1], [2], [3], [4], [5]], [[8], [4], [6], [2], [3]],[[9], [1], [2], [5], [1]]]
X1=[]
for i in range(0,len(X)):
    for j in range(0,len(X[0])):
        X1=X[i][j]
        X1.append(X)

The current output is

[1,
 [[[1, [...]], [2, [...]], [3, [...]], [4, [...]], [5, [...]]],
  [[8, [...]], [4, [...]], [6, [...]], [2, [...]], [3, [...]]],
  [[9, [...]], [1, [...]], [2, [...]], [5, [...]], [...]]]]

The expected output is

[[1,8,9], [2,4,1], [3,6,2], [4,2,5], [5,3,1]]

CodePudding user response:

Iterate over the sublists using zip, then add each inner lists, you can use list-comprehension for it:

>>> [x y z for (x,y,z) in zip(X[0], X[1], X[2])]

[[1, 8, 9], [2, 4, 1], [3, 6, 2], [4, 2, 5], [5, 3, 1]]

Or you can use reduce from functools and pass unpacked list to zip if you are not sure how many sublists are there

>>> from functools import reduce 
>>> [reduce(lambda x,y: x y, v) for v in zip(*X)]

[[1, 8, 9], [2, 4, 1], [3, 6, 2], [4, 2, 5], [5, 3, 1]]

Another approach is to call sum iterating the unpacked list passed to zip as suggested by @Mad Physicist in the comment:

>>> [sum(x, start=[]) for x in zip(*X)]

[[1, 8, 9], [2, 4, 1], [3, 6, 2], [4, 2, 5], [5, 3, 1]]

CodePudding user response:

You can use numpy instead of dealing with loops

import numpy as np
array = np.array([[[1], [2], [3], [4], [5]], [[8], [4], [6], [2], [3]],[[9], [1], [2], [5], [1]]])
// reshape and transpose
array = array.reshape(3,5).T


array will be

array([[1, 8, 9],
       [2, 4, 1],
       [3, 6, 2],
       [4, 2, 5],
       [5, 3, 1]])

CodePudding user response:

Numpy can help a lot here. First when get rid of the inner-most single-item lists, turning the three list[list[int]] into three list[int]. Then, those can be used to create a np.ndarray. We then transpose this array to get the desired result.

import numpy as np

X = [[[1], [2], [3], [4], [5]],
     [[8], [4], [6], [2], [3]],
     [[9], [1], [2], [5], [1]]]

a = [[item[0] for item in sublist] for sublist in X]
# [[1, 2, 3, 4, 5], [8, 4, 6, 2, 3], [9, 1, 2, 5, 1]]

a = np.array(a).T
# array([[1, 8, 9],
#        [2, 4, 1],
#        [3, 6, 2],
#        [4, 2, 5],
#        [5, 3, 1]])

CodePudding user response:

output = []
indexes_per_list = len(X[0])

for i in range(indexes_per_list):
    item = []
    
    for l in X:
        item.append(*l[i])
    
    output.append(item)

print(output)

CodePudding user response:

If there are fewer and more than 3 elements:

import numpy as np
x=[[[1], [2], [3], [4], [5]], [[8], [4], [6], [2], [3]],[[9], [1], [2], [5], [1]]]
 
x = np.asarray(x)
x = x.reshape(x.shape[0],x.shape[1]).T
x

Output for(3,5,1):

array([[1, 8, 9],
       [2, 4, 1],
       [3, 6, 2],
       [4, 2, 5],
       [5, 3, 1]])

Output for (4,5,1):

array([[1, 8, 9, 9],
       [2, 4, 1, 1],
       [3, 6, 2, 2],
       [4, 2, 5, 5],
       [5, 3, 1, 1]])
  • Related