2

I have a 3D, binary array/list in python which stores a sequence. I would like to iterate though the array and print out the sequence. However, being new to python, I am struggleing to set the looping variable of the inner loop to a certain value.

for r in range(9, len(resources)-9):
  for i in items:
      for j in items:
          print('assign[',i,'][',j,'][',r,'] ==', assign[(r, i)][j].SolutionValue())
          if assign[(r, i)][j].SolutionValue() ==1: 
              i=j
              print('set i=j: i = ', i)
              j=0
              print('set j=0: j = ', j)
  print()

See a partial solution below:

assign[ 0 ][ 0 ][ 9 ] == 0.0
assign[ 0 ][ 1 ][ 9 ] == 0.0
assign[ 0 ][ 2 ][ 9 ] == 0.0
assign[ 0 ][ 3 ][ 9 ] == 0.0
assign[ 0 ][ 4 ][ 9 ] == 1.0
set i=j: i =  4
set j=0: j =  0
assign[ 4 ][ 5 ][ 9 ] == 0.0
assign[ 4 ][ 6 ][ 9 ] == 0.0
assign[ 1 ][ 0 ][ 9 ] == 1.0

Inside the if statement of my code, I would like to set the i=j and return to j=0. It seems as if this works for i but not for j. Another option would be to break the inner loop, but I think there is no referencing to certain loops in python.

Thank you in advance!

Gudarzi
  • 486
  • 3
  • 7
  • 22
Hendrik
  • 153
  • 1
  • 2
  • 18
  • 1
    this [question](https://stackoverflow.com/questions/51445252/restarting-for-cycle-iterating-over-list-python3) has a good answer for that. – Miguel Aug 01 '18 at 14:03
  • 1
    I think it would be helpful if you described your original problem, it seems like there will be an easier way to solve it. – bphi Aug 01 '18 at 14:03
  • 3
    You're iterating over a sequence, not incrementing `i` and `j`. Changing those variables has only temporary effect, until the next iteration of the respective loop, when they will be assigned the next value from the sequence. – kindall Aug 01 '18 at 14:03
  • 1
    The problem with what you are doing is that you are iterating over the elements of items. You should use a variation of for/while with len(items) / range(len(items)) as the one in the link I posted before – Miguel Aug 01 '18 at 14:04
  • So you basically want to flatten the list? – whackamadoodle3000 Aug 01 '18 at 14:11

2 Answers2

1

You could use numpy to iterate through an array and print the sequence:

import numpy
for e in numpy.array([[[1,2],[1,2]],[[1,2],[1,2]]]).flatten(): print(e)

Just replace that list with your list.

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
  • I already solved my problem with the answers above, but I'd be interested in this. Can you give me an example on how to iterate through a numpy array? – Hendrik Aug 01 '18 at 14:24
  • 1
    https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.nditer.html is another way – whackamadoodle3000 Aug 01 '18 at 14:27
0

The below explanation will be a little bit generic but hopefully after reading it you'll got the basic knowledge to solve your particular or more complex problem that deal with high-dimensional arrays.

ALLOCATION (GENERAL CASE)

Let's start by reviewing a simple way of allocating space for arrays in python:

1d array: lst = [0]*K0
2d array: lst = [0]*K1*K0
3d array: lst = [0]*K2*K1*K0
4d array: lst = [0]*K3*K2*K1*K0
...
Nd array: lst = [0]*Kn*Kn-1*...*K0

The most typical dimensional arrays would be (1D, 2D, 3D, 4D) and K0/K1/K2/K3 are known as width/height/depth/time respectively.

INDEXING (GENERAL CASE)

Once you've allocated space all that remains to know is how to index these arrays, well, let's try to find out how you'd compute the index array:

1d: index = x0
2d: index = x1*k0 + x0
3d: index = x2*k1*k0 + x1*k0 + x0
4d: index = x3*k2*k1*k0 + x2*k1*k0 + x1*k0 + x0
...
Nd: index_n = xn*kn-1*kn-2*...*k0 + index_n-1

The most typical dimensional arrays would be (1D, 2D, 3D, 4D), so usually x0/x1/x2/x3 are known as x/y/z/t respectively.

PARTICULAR CASES (ALLOCATION & INDEXING)

1d: lst = [0]*width
    lst[x]
2d: lst = [0]*width*height
    lst[y*width + x]
3d: lst = [0]*width*height*depth
    lst[z*width*height + y*width + x]
4d: lst = [0]*width*height*depth*duration
    lst[t*width*height*depth + z*width*height + y*width + x]

CONCLUSION

It seems you're dealing with 3d arrays so as explained above we know at this point that one way to allocate a 3d array in python could be done like:

lst = [0]*width*height*depth

and one possible way to index such array (either to write or read it) could be:

lst[z*width*height + y*width + x]

Hope the whole explanation helps to clarify a little bit more about your concerns about arrays, be in python or any other language, the underlying theory will be always the same and the only difference is there may be other more performant ways to allocate/index arrays on those languages, that's all.

BPL
  • 9,632
  • 9
  • 59
  • 117