7

I want to convert a 3D list to a numpy array.

When i am trying to do that using np.array(list) or np.asarray(list) it is giving 2D numpy array of shape (6,10).

How can I get numpy array of 3 dimensional(like shape should be (6,10,10). Can someone help me resolving this issue.

The list i am trying to convert is mentioned below

[[[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]]
[[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L] [4L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]]
[[0L, 1L, 2L] [0L, 1L, 2L] [0L, 1L, 2L] [0L, 1L, 2L, 3L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4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 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]]]
0

3 Answers 3

3

If you build a 2D list and convert it to numpy with np.array() it will return a numpy array with 2 dimensions.

If you build a 3D list and convert it to numpy with np.array() it will return a numpy array with 3 dimensions.

Minimal working example:

my_list = [[[0,1,2],[0,1,2]],[[0,1,2],[0,1,2]]]
my_list

[[[0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2]]]

my_np_array = np.array(my_list)
my_np_array.shape

(2, 2, 3)

Sign up to request clarification or add additional context in comments.

Comments

1

That's because this is not a 3D list. List has format [item,item]; 2D list would be [[],[]]. There are no inner level separators(',') in the file above. Load it into python and you get a 6,10 tuple back, the first valid entry after which no commas, so error out.

Comments

0

Your list is not a homogeneous list, so it gives error when creating you matrix. Each dimension that u create must be equal size.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.