0

I can generate an array like this :

arr = np.array(range(0,36))
arr
#Output
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, 30, 31, 32, 33,
   34, 35])

My question is how can I generate nxn array like:

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],
[30, 31, 32, 33, 34, 35]])

1 Answer 1

1

The way you've written that output doesn't look right, bit if you mean a 6x6 array then:

>>> arr = np.array(range(0,36)).reshape((6,6))
>>> arr
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],
       [30, 31, 32, 33, 34, 35]])

Note there's an outside set of square brackets here compared to your desired output...

A slightly more compact (.reshape can take multiple args) and possible quicker way (np.arange thx @jadsq):

 np.array(np.arange(36)).reshape(6,6)
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I mean the same. Thanks.
Use np.arange instead of range, it works the same and should be much faster

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.