jeudi 4 novembre 2021

vectorizing two nested for loop with an if condition in python

I have two lists of the same size and want to check if the element of the first list is equal to the same index in the second list : here is the code that I want to vectorize.

code import numpy as np z=0

  classes =[0,1,2,3,4,5,6,7,8,9]
  a = [0, 1, 2, 2, 1, 9, 6, 5, 7, 5, 2, 3, 9, 0, 8, 4, 3, 4, 8, 6]
  b = [0, 2, 2, 1, 9, 9, 6, 3, 5, 4, 0, 3, 9, 1, 8, 4, 3, 5, 8, 6]

  for k in classes:
     for h in range(len(a)):
         if a[h]==k and b[h]==a[h]:
            z+=1
  print(z)  

Aucun commentaire:

Enregistrer un commentaire