In your array:
- The
xandt, are the beginning of the slice; - The
yandt, are the end of the slice; - The
iandm, are the step of the slice.
For example, let's define an 8x8 array:
z=[[x*y+x+y for x in range(8)] for y in range(8)]
z=np.asarray(z)
Out[1]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 1, 3, 5, 7, 9, 11, 13, 15],
[ 2, 5, 8, 11, 14, 17, 20, 23],
[ 3, 7, 11, 15, 19, 23, 27, 31],
[ 4, 9, 14, 19, 24, 29, 34, 39],
[ 5, 11, 17, 23, 29, 35, 41, 47],
[ 6, 13, 20, 27, 34, 41, 48, 55],
[ 7, 15, 23, 31, 39, 47, 55, 63]])
z.shape
Out[2]: (8, 8)
From row 0 until row 3 (excluding it) every 2 rows, will index like:
z[0:3:2]
Out[3]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 2, 5, 8, 11, 14, 17, 20, 23]])
For columns:
z[:,1:6:3]
Out[4]:
array([[ 1, 4],
[ 3, 9],
[ 5, 14],
[ 7, 19],
[ 9, 24],
[11, 29],
[13, 34],
[15, 39]])
Combining rows and columns:
z[0:3:2, 0:3:2]
Out[5]:
array([[0, 2],
[2, 8]])
Answer from Pedro on Stack Overflowpython - How to index using through 2d arrays? - Stack Overflow
Help finding the index of a given value within a 2D array
Is it possible to access multidimensional numpy array with a single index without a reshape?
Confusion about index ordering for 2d arrays.
Videos
Been messing around with numpy, trying to familiarize myself with it and seeing how I'd be able to utilize its arrays to store (very simple) map data for this text adventure game I've been working on. So far it seems like it'd be pretty darn useful, but I seem to have run into something of an issue.
My code is as follows (note: this is purposefully made with out a main class, because I am just trying to get the basic functionality down in my head before I incorporate it into my main program, and this is just easier for me):
# import numpy module as np
import numpy as np
# Establish the game map, a 3x3 grid of 0's
mainarray = np.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
# Give feedback to make console easier to read
print(mainarray)
print("")
print("Placing player in center...")
# Place player on map (represented by a value of 1)
mainarray[1, 1] = 1
print(mainarray)
print("")
print("Locating player...")
# Attempt to find what the current index is of the value 1
print("")
print("Player is at index: ", np.where(mainarray == 1)[0][0])In my head, I would like to eventually use this np.where() function (if I can) in one of the functions that moves my character. What I want to do is grab the current index of the "player" (represented by the number 1) and attempt to change that value to a 0, and change the value at an adjacent index to 1 (the tile that the player is moving to). Basically, I would like to use each index of the array as a sort of coordinate that I can take and use to move this "1" around the array, setting each index back to "0" after moving the "1", all based off user input.
Anyways, I am not getting any errors, however, the result that gets printed to the console is:
Player is at index: 1
Why is it just one number? It remains the same, even when I remove that second [0] in the last line. I don't fully understand how this function works, as this is the first time I've tried to use it, but since there are no errors, I have no clue what is going on.
Shouldn't I be receiving an index with two values, one for the column and one for the row? If not, how can I grab that as a result and use it in the way I described above? Is it even possible, or am I barking up the wrong tree with this function?
[SOLVED]
I have this piece of code
import numpy as np arr = np.array([[1, 2], [4, 5], [7, 8]]) # arr.reshape(6) print(arr[0])
which returns [1 2] as expected (please, note, that arr.reshape(6) is commented).
But is there a property or a trick to access the elements with one index without using a reshape? So I could pass argument 0 and get 1 returned:0 -> 11 -> 22 -> 43 -> 5...