1
\$\begingroup\$

Input

array([[10, 11, 12, 13, 26, 28, 11],
       [ 4,  9, 19, 20, 21, 26, 12],
       [ 1,  7, 10, 23, 28, 32, 16],
       [ 4,  6,  7, 10, 13, 25,  3],
       [ 4,  6, 15, 17, 30, 31, 16]])

test = np.array([10, 11, 12, 13, 26, 28, 11])

Trying

def check(test,array):
    for i in array:
        if np.array_equal(i,test):
            print('in')
            break 
    print('not in')

Is there any simple method without using for...in...if...else structure?

\$\endgroup\$
2
  • \$\begingroup\$ I thought that, but then I checked it and it doesn't work for numpy arrays. \$\endgroup\$
    – Josiah
    May 7 '18 at 12:56
  • \$\begingroup\$ i, obviously, didn't take my pills this morning. sorry \$\endgroup\$
    – bobrobbob
    May 7 '18 at 15:13
1
\$\begingroup\$

There are indeed ways. The most pythonic one is probably to use a generator comprehension.

def check(test,array):
    return any(numpy.array_equal(x, test) for x in array)

Note that this is not quite the same behaviour as it returns a Boolean rather than printing the result, but that behaviour should be easy enough to add outside.


As an aside, I don't like the variable name test. It sounds to me like the name of a predicate function rather than the name of an array you want to look for.

The name check is also rather too general to usefully say what it is checking.

Now I'm perhaps overly fond of slightly too descriptive names, but I'd probably be calling this def is_row_in_array(row, array):

\$\endgroup\$
3
  • \$\begingroup\$ What about any(map(lambda x:np.array_equal(x,test), array)) ? Is it more pythonic? \$\endgroup\$
    – Jack
    May 7 '18 at 14:12
  • \$\begingroup\$ That's controversial. As near as I can tell the trend is slightly to prefer comprehensions, and in particular to prefer comprehensions when you would otherwise have to define a lambda. Performance concerens also prefer comprehensions to map with a lambda. Of course some people, including myself, do find map more natural to think in. I actually wrote the map version in this answer before translating it to a generator comprehension! \$\endgroup\$
    – Josiah
    May 7 '18 at 14:38
  • \$\begingroup\$ For a bit of history about just how controversial, Guido van Rossum, the original creator of Python tried to drop map, filter and lambda from Python 3! artima.com/weblogs/viewpost.jsp?thread=98196 \$\endgroup\$
    – Josiah
    May 7 '18 at 14:57
2
\$\begingroup\$

If you don't like the looping and the if ... statements you could use solely NumPy methods:

def is_row_in_array(row , arr):
    return np.any(np.sum(np.abs(arr-row), axis=1) == 0)
\$\endgroup\$
2
  • \$\begingroup\$ (arr == row).all(axis=1).any() is shorter and a bit faster, but the general approach in this answer is right. \$\endgroup\$ May 8 '18 at 13:46
  • \$\begingroup\$ @Gareth Rees, nice improvement. \$\endgroup\$
    – Jan Kuiken
    May 8 '18 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.