0

I have a list of number

mylist = [0, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]

i converted mylist in a numpy array

import numpy as np
mylist_np = np.array(mylist)

array([ 0, 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])

i wish to give a shape to the array as:

array([[0, 1,2,3,4,5,6,7,8,9],
[10,11,12,13,14,15,16,17,18,19],
[20,21,22,23,24,25,26,27,28,29]])
2
  • Do you want to split it on every "9"? Commented Sep 6, 2015 at 11:13
  • 1
    You need to add zero then, so your rows have equal length. Commented Sep 6, 2015 at 11:16

1 Answer 1

2

Numpy does not support ragged arrays (at least, not without breaking the results of some fundamental methods)

If your array was

array([ 0, 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
   18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])

Then it could be reshaped by:

np.reshape(mylist_np, (3,10))

But why use numpy? You can shape your array when you set it up with something like:

my_list = [range(max(a,1),a+10) for a in range(0,30,10)]
Sign up to request clarification or add additional context in comments.

3 Comments

sorry but with np.reshape(mylist_np, (3,10)) i get an error message: ValueError: total size of new array must be unchanged. Did you try your code?
Yes. Did you note that you cannot do that with your current array?
oh yes sorry. I understand my mistake

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.