简体   繁体   English

使用嵌套列表检查python列表

[英]python list checking with nested list

I have two python lists.我有两个 python 列表。 For example:例如:

a = ['1', '2', '3', '4']
b =['1,2', '3,4', '3,33,34', '44']

I need to compare whether list[0] which is one is in b[0] which is 1, 2 and it has to return the output as 1 if it is present and 0 if it is not present.我需要比较为 1 的list[0]是否在b[0] ,即 1, 2 并且它必须将输出返回为 1(如果存在)和 0(如果不存在)。

The final output should be like:最终输出应该是这样的:

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)

Please do help me in writing a code for this in python as I'm a beginner in this.请帮助我在 python 中为此编写代码,因为我是这方面的初学者。

Use zip使用zip

Ex:前任:

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)

Output:输出:

1
0
1
0

You can also try the following:您还可以尝试以下操作:

for index, value in enumerate(a):  
    if value in b[index].split(","):  
        print(1)  
    else:  
        print(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM