0

I'm struggling to list my array as a 10x10 grid, the output I keep getting isn't what I'm looking for. I was hoping someone could help me out.

import numpy as np

x = 1
y = 1

scale = 10

nn = []

for x in range(1,scale+1):
    mm = []
    for y in range(1,scale+1):
        xy = [x,y]
        mm.append(xy)
        #print(xy)
        y=+1
    nn.append(mm)
    x=+1

nn


grid_array = np.array(nn)

grid=np.meshgrid(grid_array)

But the output I get isn't displaying 10x10

[array([ 1,  1,  1,  2,  1,  3,  1,  4,  1,  5,  1,  6,  1,  7,  1,  8,  1,
         9,  1, 10,  2,  1,  2,  2,  2,  3,  2,  4,  2,  5,  2,  6,  2,  7,
         2,  8,  2,  9,  2, 10,  3,  1,  3,  2,  3,  3,  3,  4,  3,  5,  3,
         6,  3,  7,  3,  8,  3,  9,  3, 10,  4,  1,  4,  2,  4,  3,  4,  4,
         4,  5,  4,  6,  4,  7,  4,  8,  4,  9,  4, 10,  5,  1,  5,  2,  5,
         3,  5,  4,  5,  5,  5,  6,  5,  7,  5,  8,  5,  9,  5, 10,  6,  1,
         6,  2,  6,  3,  6,  4,  6,  5,  6,  6,  6,  7,  6,  8,  6,  9,  6,
        10,  7,  1,  7,  2,  7,  3,  7,  4,  7,  5,  7,  6,  7,  7,  7,  8,
         7,  9,  7, 10,  8,  1,  8,  2,  8,  3,  8,  4,  8,  5,  8,  6,  8,
         7,  8,  8,  8,  9,  8, 10,  9,  1,  9,  2,  9,  3,  9,  4,  9,  5,
         9,  6,  9,  7,  9,  8,  9,  9,  9, 10, 10,  1, 10,  2, 10,  3, 10,
         4, 10,  5, 10,  6, 10,  7, 10,  8, 10,  9, 10, 10])]

Edited.

This is what I have thus far, thanks for the help guys.

import numpy as np

scale = 10
array = np.empty(shape=(scale, scale, 2)).astype(int)

for x in range(1,scale+1):
    for y in range(1,scale+1):
        #print([x,y])
        array[x-1,y-1] = [x,y] 

print(array)
Kam-ALIEN
  • 33
  • 1
  • 7

2 Answers2

2

You can use numpy to do that. like this

np.reshape(arr, (-1,10))

See. Convert a 1D array to a 2D array in numpy

Pradeep Pathak
  • 444
  • 5
  • 6
0

It's pretty far from clear what you want to achieve, but if you simply want to know how to generate a 10x10 numpy array using two for loops, here is what you can do (not he most pythonic way to do it though):

import numpy as np

scale = 10
array = np.empty(shape=(scale, scale))

for x in range(scale):
    for y in range(scale):
        array[x,y] = 42 # replace with whatever dynamically assigned value you want there    

print(array)
Karl
  • 5,573
  • 8
  • 50
  • 73
  • So what I'm trying to achieve in the long run is to make a 10x10 grid that will represent a terrain landscape that has a river flowing through it. Let's say for instance co-ordinates '3,3', '4,3' ,5'3 and 6'3 represent a river flowing through the terrain and the rest of the cells are land area. It's a little tricky to word as I'm very new to python but it's definitely achievable . – Kam-ALIEN Dec 06 '18 at 11:35
  • Ok well then you want an ndarray with shape (10,10,2). I.e. `array = np.empty(shape=(scale, scale, 2))` and then `array[x,y] = [42, 42]` – Karl Dec 06 '18 at 11:38