Gianni Spear
Gianni Spear

Reputation: 7934

give a shape to a numpy array create from simple list

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]])

Upvotes: 0

Views: 79

Answers (1)

michaelrccurtis
michaelrccurtis

Reputation: 1172

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)]

Upvotes: 2

Related Questions