0

I have the following list I want to turn into one numpy. What is the best and most effective way to do this?

[[array([1, 2, 3]), 1], [array([1, 2, 3]), 2], [array([1, 2, 3]), 4], [array([4, 4, 4]), 3]]

Expected result:

[[1, 2, 3, 1],
 [1, 2, 3, 2],
 [1, 2, 3, 4],
 [4, 4, 4, 3]]

2 Answers 2

2

You can use this

test = [[np.array([1, 2, 3]), 1], 
        [np.array([1, 2, 3]), 2], 
        [np.array([1, 2, 3]), 4], 
        [np.array([4, 4, 4]), 3]]

np.apply_along_axis(lambda x:np.hstack((x[0],[x[1]])),axis=1,arr=test)
Sign up to request clarification or add additional context in comments.

Comments

0
import numpy as np

arrays = [[np.array([1, 2, 3]), 1], [np.array([1, 2, 3]), 2], [np.array([1, 2, 3]), 4], [np.array([4, 4, 4]), 3]]

new_array = np.empty([0, 4])
for i in range(len(arrays)):
    new_array = np.vstack((new_array, np.append(arrays[i][0], arrays[i][1])))
    
print(new_array)
array([[1., 2., 3., 1.],
       [1., 2., 3., 2.],
       [1., 2., 3., 4.],
       [4., 4., 4., 3.]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.