Hi my question is in regards to the J.F. Sebastian code and discussion below.
I am trying to print a sub array of an array. The code is below and my update is on the bottom. (It gives error messages)creating a spiral array in python?
I want to create a sub array based on a chosen entry in this array. if we choose the 04 entry,
subarray[x][y] == 04
subarray[x][y] == TheSpiral[x-1:x+2,y-1:y+2]
06 01 02
05 04 03
16 15 14
The issue is that the second slice doesn't do what you think it does
, however ...
You can fix it with a list comprehension:
>>> i, j = 3, 2 >>> subarray = [row[j-1:j+2] for row in TheSpiral[i-1:i+2]] >>> print_matrix(subarray) 06 01 02 05 04 03 16 15 14
Alternatively you can use
numpy (which maybe overkill!!!) and use your original syntax:
>>> import numpy as np >>> subarray = np.array(TheSpiral)[i-1:i+2,j-1:j+2] >>> print_matrix(subarray) 06 01 02 05 04 03 16 15 14