0

I have some arrays m rows by 2 `columns (like series of coordinates) and I want to automatize my code so that I will not use nested loop for every coord. Here is my code it runs well and gives right answer coordinates but I want to make a dynamic loop:

import numpy as np

A = np.array([[1,5,7,4,6,2,2,6,7,2],[2,8,2,9,3,9,8,5,6,2],[3,4,0,2,4,3,0,2,6,7],\
 [1,5,7,3,4,5,2,7,9,7],[6,2,8,8,6,7,9,6,9,7],[0,2,0,3,3,5,2,3,5,5],[5,5,5,0,6,6,8,5,9,0]\
 ,[0,5,7,6,0,6,9,9,6,7],[5,5,8,5,0,8,5,3,5,5],[0,0,6,3,3,3,9,5,9,9]]) 
    
number = 8292 
number = np.asarray([int(i) for i in str(number)]) #split number into array 

#the coordinates of every single value contained in required number
coord1=np.asarray(np.where(A == number[0])).T
coord2=np.asarray(np.where(A == number[1])).T
coord3=np.asarray(np.where(A == number[2])).T
coord4=np.asarray(np.where(A == number[3])).T

coordinates = np.array([[0,0]])  #initialize the array that will return all the desired coordinates 
solutions = 0   #initialize the array that will give the number of solutions

for j in coord1:
    
   j = j.reshape(1, -1)

   for i in coord2 :
    
    i=i.reshape(1, -1)
    
    if (i[0,0]==j[0,0]+1 and  i[0,1]==j[0,1]) or (i[0,0]==j[0,0]-1 and  i[0,1]==j[0,1]) or (i[0,0]==j[0,0] and  i[0,1]==j[0,1]+1) or (i[0,0]==j[0,0] and  i[0,1]==j[0,1]-1) :
         
        for ii in coord3 :
            ii=ii.reshape(1, -1)
            
            if (np.array_equal(ii,j)==0 and ii[0,0]==i[0,0]+1 and  ii[0,1]==i[0,1]) or (np.array_equal(ii,j)==0 and ii[0,0]==i[0,0]-1 and  ii[0,1]==i[0,1]) or (np.array_equal(ii,j)==0 and ii[0,0]==i[0,0] and  ii[0,1]==i[0,1]+1) or (np.array_equal(ii,j)==0 and ii[0,0]==i[0,0] and  ii[0,1]==i[0,1]-1) :  
             
             for iii in coord4 :
               iii=iii.reshape(1, -1)
               if (np.array_equal(iii,i)==0 and  iii[0,0]==ii[0,0]+1 and  iii[0,1]==ii[0,1]) or (np.array_equal(iii,i)==0 and iii[0,0]==ii[0,0]-1 and  iii[0,1]==ii[0,1]) or (np.array_equal(iii,i)==0 and iii[0,0]==ii[0,0] and  iii[0,1]==ii[0,1]+1) or (np.array_equal(iii,i)==0 and iii[0,0]==ii[0,0] and  iii[0,1]==ii[0,1]-1) :  
                        point = np.concatenate((j,i,ii,iii)) 
                        coordinates = np.append(coordinates,point,axis=0)
                        solutions +=1
             
                        
coordinates = np.delete(coordinates, (0), axis=0)
Amir Afianian
  • 2,679
  • 4
  • 22
  • 46
  • 1
    please provide an [mre] It should at least include, an example to recreate your problem: sample input, current output, expected output. – Andreas Oct 31 '20 at 21:00
  • my question is a little bit similar to this one https://stackoverflow.com/questions/38276007/how-to-define-dynamic-nested-loop-python-function since i have a function and depending in the inputs for that function i should execute n for loops you got it ? – Juan Sebastien Veron Oct 31 '20 at 21:19

1 Answers1

1
import itertools

A = [1, 2, 3]
B = [4, 5, 6]
C = [7, 8, 9]

for (a, b, c) in itertools.product (A, B, C):
    print (a, b, c);

outputs:

1 4 7
1 4 8
1 4 9
1 5 7
1 5 8
1 5 9
1 6 7
1 6 8
1 6 9
2 4 7
2 4 8
2 4 9
2 5 7
2 5 8
2 5 9
2 6 7
2 6 8
2 6 9
3 4 7
3 4 8
3 4 9
3 5 7
3 5 8
3 5 9
3 6 7
3 6 8
3 6 9

See documentation for details.

Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40