我有两个python列表。例如:
a = ['1', '2', '3', '4']
b =['1,2', '3,4', '3,33,34', '44']
我需要比较list[0]
中是否为b[0]
中的1、2,如果存在则必须将输出返回为1,如果不存在则必须将输出返回为0。
最终输出应为:
1 (as 1 is present in 1,2)
0 (as 2 is not present in 3,4)
1 (as 3 is present in 3,33,34)
0 (as 4 is not present in 44)
作为我的初学者,请帮我用python编写代码。
答案 0 :(得分:1)
使用zip
例如:
a = ['1','2','3','4']
b =['1,2', '3,4', '3,33,34', '44']
for i, v in zip(a, b):
if i in v.split(","): #Check if element in b
print(1)
else:
print(0)
输出:
1
0
1
0
答案 1 :(得分:1)
您还可以尝试以下操作:
for index, value in enumerate(a):
if value in b[index].split(","):
print(1)
else:
print(0)