samedi 12 septembre 2020

How to find how many values are divisible in to certain value in 2d array in python

The following code generate random number of 2d arrays and I want to print how many values in each pair are divisible to 3. For example assume we have an array [[2, 10], [1, 6], [4, 8]]. So the first pair which is [2,10] has 3 ,6 and 9 which are totally 3 and second pair has 3 and 6 which are totally two and last pair[4,8] has just 1 divisible to 3 which is 6. Therefore, The final out put should print sum of total number of divisible values which is 3+2+1=6

a=random.randint(1, 10)

b = np.random.randint(1,10,(a,2))
b = [sorted(i) for i in b]   
c = np.array(b)            
   
counter = 0;   
  
for i in range(len(c)): 
    d=(c[i,0],c[i,1])
    if (i % 3 == 0):  
        counter = counter + 1
print(counter)



Aucun commentaire:

Enregistrer un commentaire