Home > Back-end >  I want to use numpy to make a 5 x 7 grid with spaces rather than 0s
I want to use numpy to make a 5 x 7 grid with spaces rather than 0s

Time:12-18

I am trying to make a grid with spaces that works the the same as a grid with 0

The fact that the are 0 in the grid is irrelevant to my code. I could make a grid of 1s 2s or anything else and the main body of code would still work as long as I change a few thing that are expecting 0s. This is my present code:

import numpy as np
def make_grid():
    board = np.zeros((5,7))
    return board
grid = make_grid()
print(grid)

this is what i am getting:

[[0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]]

this is what i want to get:

[[ .  .  .  .  .  .  .]
 [ .  .  .  .  .  .  .]
 [ .  .  .  .  .  .  .]
 [ .  .  .  .  .  .  .]
 [ .  .  .  .  .  .  .]]

also i have this function:

def location(grid, col):
    return grid[4][col] == 0

what do i change the 0 to once I implement your code as before it was for checking if the column was full but if the grid is space i can't just do a " " or a tap of the space bar can I, to replace the 0.

@Hanna code does this:

[[' ' ' ' ' ' ' ' ' ' ' ' ' ']
 [' ' ' ' ' ' ' ' ' ' ' ' ' ']
 [' ' ' ' ' ' ' ' ' ' ' ' ' ']
 [' ' ' ' ' ' ' ' ' ' ' ' ' ']
 [' ' ' ' ' ' ' ' ' ' ' ' ' ']
 [' ' ' ' ' ' ' ' ' ' ' ' ' ']]

can i make this:

[[' '' '' '' '' '' '' ']
 [' '' '' '' '' '' '' ']
 [' '' '' '' '' '' '' ']
 [' '' '' '' '' '' '' ']
 [' '' '' '' '' '' '' ']
 [' '' '' '' '' '' '' ']]

CodePudding user response:

If you're trying to get the particular output with dots and spaces without quotes on the screen, try to define your own print function :)

import numpy as np
def make_grid():
    board = np.full((5, 7), ' ')
    return board
grid = make_grid()

def my_print(grid):
    rows, cols = grid.shape
    for i in range(rows):
        print('[', end='')
        for j in range(cols):
            print(grid[i, j], end='.')
        print(']')

my_print(grid)

"""
[ . . . . . . .]
[ . . . . . . .]
[ . . . . . . .]
[ . . . . . . .]
[ . . . . . . .]
"""

CodePudding user response:

To create a grid of spaces instead of zeros, you can simply replace the call to np.zeros with a call to np.empty and pass in the desired shape of the grid as arguments. The np.empty function creates an array with uninitialized values, so the elements of the array will be spaces by default. Here is how you can modify your make_grid function to create a grid of spaces:

import numpy as np

def make_grid():
    board = np.empty((5,7))
    return board

grid = make_grid()
print(grid)

This will print the following grid:

[[  .   .   .   .   .   .   .]
 [  .   .   .   .   .   .   .]
 [  .   .   .   .   .   .   .]
 [  .   .   .   .   .   .   .]
 [  .   .   .   .   .   .   .]]

To modify the location function to work with a grid of spaces, you can simply change the comparison operator to check if the element is equal to a space character instead of zero. Here is how you can modify the location function:

def location(grid, col):
    return grid[4][col] == ' '

This will return True if the element at the specified location is a space, and False if it is not.

Keep in mind that using a grid of spaces will not work with certain operations that expect numerical values, such as arithmetic or statistical functions. In those cases, you may need to convert the grid to a numerical format before performing the operation.

  • Related