-1

I am learning Python and trying to compare the elements of multiple arrays/lists at a time, my arrays are Numpy arrays (I guess?) as I did the following to create them and the answer mentions the output as a numpy array

I have 3 arrays of length 40 in total and what I want to do is iterate through them and check if elements at the same index are equal, in a programming language such as Java I can easily do this using something like:

for (int i=0;i<40; i++)
   if(array1[i]==array2[i]==array3[i] 
     //do whatever

however I am having trouble doing this as tutorials on iterating through the array will be something like this:

for x in arr:
  \\do work

with no reference to an index, any help would be appreciated

UPDATE: for more clarification, I used the linked answer to import a csv file with strings to a numpy array, so my array elements are strings

here is what print(array) outputs, I have 3 of those

enter image description here

Sergio
  • 275
  • 1
  • 15
  • 1
    `for i, x in enumerate(arr):` is a good way to get the index and item. If you just need the index `for i in range(len(arr)):`, but that is considered "unpythnic" if you just want to iterate one `list` (that's the real name for what you call "array"). – Klaus D. May 05 '21 at 08:55
  • 1
    But much better implementation will be `for ar1element, ar2element, ar3element in zip(array1, array2, array3)` – Olvin Roght May 05 '21 at 08:59
  • What happened when you tried putting `how does a for loop work in python` into a search engine? Or looking for and following any kind of tutorial? Or anything remotely like that? And if you don't understand how to do something as basic as this, why are you trying to use as complex of a library as Numpy? – Karl Knechtel May 05 '21 at 09:18
  • It seems you know the word `iterate`, and that you're aware that what you want to do is iterate through Numpy arrays in parallel. So maybe you could try a more tailored search on that basis. – Karl Knechtel May 05 '21 at 09:19

4 Answers4

2

This should work for you:

for x,y,z in zip(array1,array2,array3):
    if(x==y and x==z):
        #Do Something
python_ged
  • 600
  • 2
  • 10
1

You can get the index by enumerating the list.

for idx, val in enumerate(arr):
    print(idx, val)
Astik Gabani
  • 599
  • 1
  • 4
  • 11
1

Simply use zip() function.

num1 = [1, 2, 3]
num2 = [3, 4, 35]
num3 = [21, 24, 43]

for n1, n2, n3 in zip(num1, num2, num3):
  print(n1, n2, n3)

Output:

1 3 21
2 4 24
3 35 43
  • this method did not work as I said in another comment, I got the following : "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" the arrays have strings as elements if that matters – Sergio May 05 '21 at 09:06
1
import numpy as np    
where_equal = np.where(np.logical_and(array1==array2, array2==array3))
for i,j in zip(*where_equal):
    #do stuff... for example
    print(array1[i,j])

https://numpy.org/doc/stable/reference/generated/numpy.logical_and.html

https://numpy.org/doc/stable/reference/generated/numpy.where.html

zip(*where_equal) unpacks the tuple of indices returned by np.where and then zips into an iterable.