Home > Blockchain >  How do I check the first index of an array of arrays
How do I check the first index of an array of arrays

Time:03-30

Say I have a value...

x = 5

and a numpy array of arrays which looks something like...

arr = [[1,2], [3,4], [5,6]]

how would I check the first index of each array (ie. 1, 3, 5) to see if they = x?

CodePudding user response:

You may find any() function useful in this case. It takes a list as an argument, and returns True if any of their arguments evaluate to True (and False otherwise).
Here's how you do

if any([i[0] == x for i in arr]):

CodePudding user response:

You can map a lambda function to your array:

result = list(map(lambda y: y[0] == x, arr))

This returns

[False, False, True]

CodePudding user response:

Try this

for i in arr:
    if i[0]==x:
      print('found it')

CodePudding user response:

From my first comment, the most straightforward answer would be slicing:

arr[:,0]==x

Which would give you

array([False, False, True])
  • Related