1

Defined a 1d array (or a 1d slice of a bigger array), like:

a=np.array([ 5, 12, 13])

if there some higher dimension array, like:

c1=np.array([[1, 2, 3], [ 5, 12, 13], [7, 8, 9]])
c2=np.array([[1, 2, 3], [ 5, 6, 7], [7, 8, 9]])

It turns out to be:

a in c1, a in c2
(True, True)

I would like that only the first condition, where a is consecutively contained as sub-array to be True. While a in c2 would give False. Is there any function taking care of that?

gluuke
  • 1,179
  • 6
  • 15
  • 22
  • Isn't the first `True` not just accidentally true? If you change a to `a=np.array([ 5, 12, 13])` the output is `(True, False)` and should be `(False, False)`. –  Jun 02 '14 at 16:53
  • mmmh ... I just edit the formatting in case it was misleading (some extra space). However I don't get your point @andi. – gluuke Jun 02 '14 at 16:58
  • Your code returns true when only one element of a is found in c1 or c2. But I think you want to check if the entire array is found in c1 or c2. Am I wrong? –  Jun 02 '14 at 17:04
  • 1
    Expected output for `a in c1.T` ? – wim Jun 02 '14 at 17:17
  • Possible duplicate of [testing whether a Numpy array contains a given row](http://stackoverflow.com/q/14766194/2379410) –  Jun 03 '14 at 07:54
  • @andi: exactly, I want to check if the entire array `a` is present as an "element" contained in the array-of-array. It is indeed more or less what was already posted, thanks @moarningsun. – gluuke Jun 03 '14 at 14:08

2 Answers2

2

I think, this is what you want:

import numpy as np

a=np.array([ 5, 12, 13])

c1=np.array([[1, 2, 3], [5,12,13], [7, 8, 9]])
c2=np.array([[1, 2, 3], [5,6,7], [7, 8, 9]])

print any((a == x).all() for x in c1)
print any((a == x).all() for x in c2)

It outputs:

True
False

Edit: as moarningsun suggested, here a better version:

import numpy as np

a=np.array([ 5, 12, 13])

c1=np.array([[1, 2, 3], [5,12,13], [7, 8, 9]])
c2=np.array([[1, 2, 3], [5,6,7], [7, 8, 9]])

print np.any((a == c1).all(axis=1))
print np.any((a == c2).all(axis=1))
  • Although this will return true if the sub-array being searched for 'wraps' around to the next row. – fiveclubs Jun 02 '14 at 17:03
  • Is this not what he wanted? –  Jun 02 '14 at 17:06
  • 2
    `np.all` takes an "axis" parameter, which is more efficient than the generator expression you have now –  Jun 02 '14 at 17:07
  • Sorry, I was not aware of this. Would you mind editing the answer? I would also be very interested. –  Jun 02 '14 at 17:08
  • 2
    It's just `np.any((a == c1).all(axis=1))`. It "broadcasts" `a` to the shape of `c1`. –  Jun 02 '14 at 17:18
  • @ moarningsun: If I modify code acordingly, I get not the desired output, but rather twice " at 0x0000000009476090>" –  Jun 02 '14 at 23:55
  • Oh but you don't need to modify the code, you can just paste my code "as is" in your interpreter and you'll see the desired output `:)` –  Jun 03 '14 at 07:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54965/discussion-between-moarningsun-and-andi). –  Jun 03 '14 at 07:55
  • in the EDIT by @moarningsun there is a mispelled "import" at the beginning of each line, if someone can remove it, thanks. – gluuke Jun 03 '14 at 14:10
  • I confirm that the edit is necessary, because I am looking for the exact occurence of that subarray (as a row in this case) into the 2d one. PS: there is still one "a" to remove in the beginning of the line. – gluuke Jun 03 '14 at 14:19
2

You can use .tolist() and then call the functions normally:

>>> a=np.array([ 5, 12, 13])
>>> c1=np.array([[1, 2, 3], [ 5, 12, 13], [7, 8, 9]])
>>> c2=np.array([[1, 2, 3], [ 5, 6, 7], [7, 8, 9]])
>>> a.tolist() in c1.tolist(), a.tolist() in c2.tolist()
(True, False)
>>> 
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76