Tuesday, 15 April 2014

Array indexing inconsistent between script and console Python 2.7? -


I'm setting up a 3D box, 2x2x3 in this example. I have used the coordinates of the centroids of each 1x1x1 cell in the box to understand a list (I'm using Python 2.7). I then convert that list structure into a narrow array and randomly fill the half box.

Import as np tube == [] xy = 2 in z category (3): tube. For the range (xy)] for X in the range (XI) (attach (0.5 + 0, 0.5 + 0, 0.5 + 0, 0, 0, 0, 0, 0, 0, 0, 0)) centroid = Np.array (tube) exitFlag = incorrect for numpart in range (6): while exitFlag == false: zCoord = np.random.randint (3, size = 1) xyCoord = np.random.randint ((xy * Xy), size = 1) print zCoord, xyCoord if nucleus [zCoord] [XyCoord] [3] == 0.0: centroid [zCoord] [xyCoord] [3] = 1.0 exitFlag = True print centroid

I am happy with the size of the array but I am getting the following error,

  if centroid [zCoord] [xyCoord] [3] == 0.0: index error: index 3 axis From range to 1 Outside is 1 size  

Now if I ask for the exact same entry in the console then I do not get any pointer error.

For example, if the Randant call 0 and 3 related Hello, I get the above error

However, when I press the centroid [0] [3] [3] in the console ] , I get 0.0 , which I hope.

nucleus [zCoord] [XyCoord] [3] < / Code> This is not a good method of an array index, sometimes it works, but this is not an intention mechanism.

A set of brackets gives more control:

  if centroid [zCoord, xyCoord, 3] == 0: Centroid [zCoord, xyCoord, 3] = 1 exitFlag = True  

I added

  print nucleus [zCoord] .shape print centroid [zCoord] [XyCoord]  

And (1, 4, 4) after the error message ZCoord you generate it is not a scalar, but the size (1,) . Therefore, with the sequentially leaving the initial 1 dimension, the second bracket then tries to index that dimension, which is not expected to be the second of your expectations. The reason for your console test work is that you are using scalers, not an array.


No comments:

Post a Comment