小编use*_*584的帖子

如何查看一个列表中的所有项目是否都在python的第二个列表中?

我是python的新手,从Python和一些谷歌搜索的短篇课程中把它放在一起.我正在尝试比较两个字符串列表,以查看列表A的所有项目是否在列表B中.如果列表B中没有任何项目,我希望它打印通知消息.

List_A = ["test_1", "test_2", "test_3", "test_4", "test_5"]
List_B = ["test_1", "test_2", "test_3", "test_4"]
Run Code Online (Sandbox Code Playgroud)

码:

for item in List_A:
     match = any(('[%s]'%item) in b for b in List_B)
     print "%10s %s" % (item, "Exists" if match else "No Match in List B")
Run Code Online (Sandbox Code Playgroud)

输出:

test_1列表B中没有匹配项

test_2列表B中没有匹配项

test_3列表B中没有匹配项

test_4列表B中没有匹配项

test_5列表B中没有匹配项

前四个应匹配但不匹配,第五个是正确的.我不知道它为什么不起作用.有人能告诉我我做错了什么吗?任何帮助将不胜感激.

python comparison list python-2.7

1
推荐指数
1
解决办法
165
查看次数

标签 统计

comparison ×1

list ×1

python ×1

python-2.7 ×1