-3

I want to compare two ordered numpy arrays of int of the same size in Python and output the common elements which are the same value at the same position :

import numpy as np
a = np.asarray([20, 35, 226, 62, 129, 108, 156, 225, 115, 35, 162, 43, 9, 120, 181, 220])
b = np.asarray([1, 35, 69, 103, 137, 171, 205, 239, 18, 52, 86, 120, 154, 188, 222, 240])

The element-wise comparison would give : [35]

Can you help me ?

SebMa
  • 4,037
  • 29
  • 39
  • 1
    Are you looking for two values that are at the same index in both lists? – chngzm Sep 06 '17 at 12:56
  • _"The real intersection is `[35]`"_ - No, both `35` and `120` appear in both lists. Do you want values that are the same in each list's index? – Christian Dean Sep 06 '17 at 12:56
  • `[a1 for a1,b1 in zip(a,b) if a1==b1]` – Chris_Rands Sep 06 '17 at 12:57
  • @ChristianDean No, the item `120` does not have the same position. The order is imported here. – SebMa Sep 06 '17 at 13:06
  • @BradSolomon No, the item 120 does not have the same position. The order is imported here. – SebMa Sep 06 '17 at 13:06
  • 1
    @SebMa Yes, but [the mathematically definition of a intersection](https://en.wikipedia.org/wiki/Intersection_(set_theory)) of two set's are elements that appear in both the first set and the second set. I'm trying to show you that you're using the wrong terminology here. – Christian Dean Sep 06 '17 at 13:08
  • @ChristianDean You are absolutely right but here I'm not dealing with sets. I guess `intersection` is not the right word to use here :) – SebMa Sep 06 '17 at 13:10
  • "is not the right word to use here" really minimize too much how bad is the word `intersection` in the context of your problem. – Antonio Ragagnin Jul 07 '18 at 14:10
  • @AntonioRagagnin Do you what word I should use, if so, please tell me ? – SebMa Jul 07 '18 at 20:17
  • @SebMa, element-wise equality, see: https://docs.scipy.org/doc/numpy/reference/generated/numpy.equal.html – Antonio Ragagnin Jul 08 '18 at 13:05

4 Answers4

3

If you're using NumPy, than you can use a boolean mask:

import numpy as np 
a = np.asarray([20, 35, 226, 62, 129, 108, 156, 225, 115, 35, 162, 43, 9, 120, 181, 220])
b = np.asarray([1, 35, 69, 103, 137, 171, 205, 239, 18, 52, 86, 120, 154, 188, 222, 240])
c = a[a == b]
print(c) # [35]
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
2

You apparently don't need a set intersection. Zip the lists and compare items at the same index:

>>> [x for x, y in zip(a, b) if x==y]
[35]
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • 1
    @ChristianDean If they are of different sizes, `zip` still works since they are checking for equality of items at the same index. Indices that are non existent won't matter. – Moses Koledoye Sep 06 '17 at 13:00
  • Ah, yes your right. I didn't see that at first. Note to self: don't try to program as soon as you wake up. – Christian Dean Sep 06 '17 at 13:02
0

Something like this can work:

l = []
for x,y in zip(a,b):
   if x == y: 
      l.append(x)

in terms of list comprehension in can be written like this:

l = [x for x,y in zip(a,b) if x == y]

Explanation

zip(a,b) will generate the following:

>>> zip(a,b)
[(20, 1), (35, 35), (226, 69), (62, 103), (129, 137), (108, 171), (156, 205), (225, 239), (115, 18), (35, 52), (162, 86), (43, 120), (9, 154), (120, 188), (181, 222), (220, 240)]
>>>

Then, you iterate through each element (x,y) of the results of zip(a,b) and compare x and y .

Reproducible example:

>>> a = [20, 35, 226, 62, 129, 108, 156, 225, 115, 35, 162, 43, 9, 120, 181, 220
>>> b = [1, 35, 69, 103, 137, 171, 205, 239, 18, 52, 86, 120, 154, 188, 222, 240
>>> zip(a,b)
[(20, 1), (35, 35), (226, 69), (62, 103), (129, 137), (108, 171), (156, 205), (2
>>> [x for x,y in zip(a,b) if x == y ]
[35]
>>>
Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117
0

something like that will do the job, though using zip is more pythonic ...

for index, value in enumerate(a):
if value == b[index]:
    c.append(value)
A.Joly
  • 2,317
  • 2
  • 20
  • 25