Reputation: 43
For example, I have two lists like
a = ["there", "is", "a", "car"]
b = ["i", "feel", "happy", "today"]
I want to compare a[0]
and b[0]1, if there is any common alphabet between
'there'and
'i'` the result should be true or else it should be false
Output:
[False,False,True,True]
I was able to do it if it is just one element in a and b but cannot iterate through the list
a = ["there", "is", "a", "car" , "jill"]
b = ["i", "feel", "happy", "today" ,"jill"]
d = []
i = 0
for word in range(len(a)):
for word in range (len(b)):
c = list(set(a[i]) & set(b[i]))
if c == []:
d.append(False)
else:
d.append(True)
i = i+1
print (d)
Upvotes: 4
Views: 392
Reputation: 1074
Other's answers are good enough, here is your version fixed:
a = ["there", "is", "a", "car" , "jill"]
b = ["i", "feel", "happy", "today" ,"jill"]
d = []
i = 0
for word in range(len(a)):
for word in range (len(b)):
c = list(set(a[i]) & set(b[i]))
if c == []:
d.append(False)
else:
d.append(True)
i = i+1 # <------------------------ this was missing an indentation
print (d)
Upvotes: 1
Reputation: 2055
Assuming you want to perform the tests in pairs, this is your code:
print([bool(set(x) & set(y)) for (x, y) in zip(a, b)])
Your input lists are of unequal length, so it is not clear to me what you want to do with "jill" (the b item unmatched if a).
A bit more details:
Upvotes: 4
Reputation: 23528
Something like this should work:
d = [len(set(i)&set(j)) > 0 for i,j in zip(a,b)]
Testing:
>>> a = ["there", "is", "a", "car" , "jill"]
>>> b = ["i", "feel", "happy", "today" ,"jill"]
>>> d = [len(set(i)&set(j)) > 0 for i,j in zip(a,b)]
>>> d
[False, False, True, True, True]
>>>
Upvotes: 3