Reputation: 7490
I have two arrays as follows:
e = np.array([True, False, True, True])
f = np.array([True, False, False, True])
I want to get the index i
where e[i] and f[i] == True
.
The expected output for the above will be:
[0,3] since e[0] == True and f[0] == True; e[3] and f[3] ==True
There may be more of such matches, so I need a list of all of the indices which satisfy the above condition.
I tried to find the list of matching values by doing this:
list(e&f)
Out[474]:
[True, False, False, True]
To get the index, I thought I could use .index(True)
of list
. But it doesn't work or just gives output as 0. Maybe only giving the first index and not all.
Finally, I need the 1 added to each element of output so instead of [0,3]
output should be [1,4]
but this is easy I can do that if I get the indices,
Upvotes: 0
Views: 77
Reputation: 50
Something else if you don't want to be dependent on numpy:
e=[True, False, True, True]
f=[True, False, False, True]
for idx, val in enumerate(e):
if cmp(e[idx], f[idx]) is 0:
print idx+1, val
Upvotes: 1
Reputation: 4559
Take a look at numpy.where in the docs
np.where(e&f)[0]
Outputs:
array([0, 3])
Upvotes: 3
Reputation: 87064
You can just use a list comprehension to pick them out:
>>> e = np.array([True, False, True, True])
>>> f = np.array([True, False, False, True])
>>> [i for i,v in enumerate(e&f, 1) if v]
[1, 4]
Using enumerate()
you can specify the initial index, in this case 1.
Upvotes: 1