2

How do I convert my list into an array of arrays? I'm not sure if I used the correct wording, but:

Here's an example:

What I have:

[[3948,1234,4928,9283,9238]]

What I am trying to have:

[[3948],
 [1234],
 [4928],
 [9283],
 [9238]]```

Thanks Guys!
7
  • Just transpose your array. Commented Mar 23, 2021 at 20:58
  • What do you mean by "an array of arrays"? You've tagged this with numpy, but your output is just another regular list object. Commented Mar 23, 2021 at 20:59
  • What did np.array(alist) produce? Do you know about reshape? Or transpose? Commented Mar 23, 2021 at 20:59
  • Look at this answer: Numpy array of numpy arrays Commented Mar 23, 2021 at 21:01
  • @kcw78, I don't think he means an object dtype. It looks more like an array shape issue. Commented Mar 23, 2021 at 21:10

3 Answers 3

3
import numpy as np
d = np.array([3948,1234,4928,9283,9238])
dd =d.reshape((5,1))
print(dd.shape)
(5, 1)
print(dd) 

Output

[[3948]
 [1234]
 [4928]
 [9283]
 [9238]]  
Sign up to request clarification or add additional context in comments.

Comments

1

This will work fine:

t = [[3948,1234,4928,9283,9238]]
k = [[i] for i in t[0]]
print(k)

Output

[[3948], [1234], [4928], [9283], [9238]]

Comments

0

You could use

array_of_arrays = [[num] for num in array]

Which creates a new list by going through every value of your previous list and adds [value] to it.

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.