Reputation: 31
so im given two lists
lst1 =[0,1,1,1,0]
lst2 =[0,0,1,1,0]
and i need to see which index in both lists have the value 1 here is my code so far
x = list(zip(lst1,lst2))
for i in range(len(x)):
flag = 0
for y in range(len(x[i])):
if x[i][y] == 1:
flag +=1
if flag == 2:
z = x.index(x[i])
print(z)
but this prints the index 2 and 2 instead of 2 and 3 can anyone point out the problem here, thanks
Upvotes: 0
Views: 2176
Reputation: 12515
You could also use NumPy's element-wise & operation, as well as argwhere
for this:
>>> np.argwhere((np.array(lst1) == 1) & (np.array(lst2) == 1)).ravel()
array([2, 3])
Upvotes: 0
Reputation: 8298
You could do the following:
lst1 =[0,1,1,1,0]
lst2 =[0,0,1,1,0]
assert len(lst1) == len(lst2)
idx = [i for i in range(len(lst1)) if lst1[i] == 1 and lst2[i] == 1]
print(idx)
Other solution using numpy
is:
import numpy as np
lst1 =[0,1,1,1,0]
lst2 =[0,0,1,1,0]
assert len(lst1) == len(lst2)
lst1_ = np.array(lst1)
lst2_ = np.array(lst2)
idx_ = np.intersect1d(np.where(lst1_ == 1)[0],np.where(lst2_ == 1)[0])
print(list(idx_))
Other alternative is to switch the following line:
idx_ = np.intersect1d(np.where(lst1_ == 1)[0],np.where(lst2_ == 1)[0])
By:
idx_ = np.where((lst1_==1)&(lst2_==1))[0]
As stated by @yatu is to use bitwise operations.
Upvotes: 1
Reputation: 743
Look, simple iterate through both lists. Suppose you know length of both lists, then just do following:
lst1 =[0,1,1,1,0]
lst2 =[0,0,1,1,0]
# as we know length of the both lists, and their length are equal,
# i'll just write length as 5, but you can have other algorhitm of finding length
list_len = 5
is_there_any_matches = False
for index in range(list_len):
if lst1[index] == lst2[index]:
is_there_any_matches = True
break # to exit after first match
Note that this will break cycle after first match, but you can remove break
, and count number of matches instead. Also, always take length of smaller list to prevent script from error with list index out of range. Have a nice time!
EDIT
I tried to make it as simple as possible, but you can use generators, or other pythonic tricks to make it shorter and more convenient use.
Upvotes: 0
Reputation: 11
Many answers available on how to do it better.
With respect to your code snippet, List.index always gives the first matching element in the list. That's the reason we are 2,2
>>> lst1 =[0,1,1,1,0]
>>> lst2 =[0,0,1,1,0]
>>> x = list(zip(lst1,lst2))
>>> x
[(0, 0), (1, 0), (1, 1), (1, 1), (0, 0)]
>>> x.index((1,1))
2
>>> x.index((1,1))
2
Upvotes: 1
Reputation: 2368
You could do this with element-wise and
:
l = [i for i, _ in enumerate(lst1 and lst2) if _ == 1]
With the and
function, only in cases where both lists' elements have a value of 1, will the expression return 1, which seems perfect for your question.
Upvotes: -1
Reputation: 12515
Assuming they're of the same length, you could use this:
>>> [i for i, (x, y) in enumerate(zip(lst1, lst2)) if x == y == 1]
[2, 3]
Upvotes: 2
Reputation: 9494
You can iterate the over the values pairs and their index in one loop:
for idx, (i, j) in enumerate(zip(lst1, lst2)):
if i == j == 1:
print(idx)
Upvotes: 0