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)
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:
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]
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With