I'm trying to compare two lists and simply print a message if any value from the first list is in the second list.
def listCompare():
list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]
if list1 in list2:
print("Number was found")
else:
print("Number not in list")
In this example, I want the if
to evaluate to True
because 5 is in both lists. This doesn't work, and I'm not sure of the simplest way to compare the two lists.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…