Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何检查一个列表中的所有项是否都在第二个列表中?_Python_List_Python 2.7_Comparison - Fatal编程技术网

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

在python中,如何检查一个列表中的所有项是否都在第二个列表中?,python,list,python-2.7,comparison,Python,List,Python 2.7,Comparison,我是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"] 代码: 输出: 测试1在列表B中不匹配 测试2在列表B中不匹配 测试3列表

我是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"]
代码:

输出:

测试1在列表B中不匹配

测试2在列表B中不匹配

测试3列表B中没有匹配项

测试4列表B中没有匹配项

测试5列表B中没有匹配项

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

>>> '[%s]'%"test_1"
'[test_1]'
您正在检查“[test_1]”是否是
列表B
中某个字符串的子字符串,依此类推

这应该起作用:

for item in List_A:
     match = any(item in b for b in List_B)
     print "%10s %s" % (item, "Exists" if match else "No Match in List B")
但是,由于您并不是真正在寻找子字符串,因此您应该在中测试
,仅此而已:

for item in List_A:
     match = item in List_B
     print "%10s %s" % (item, "Exists" if match else "No Match in List B")
但您可以简单地使用集合差分:

print set(List_A) - set(List_B)
您正在检查“[test_1]”是否是
列表B
中某个字符串的子字符串,依此类推

这应该起作用:

for item in List_A:
     match = any(item in b for b in List_B)
     print "%10s %s" % (item, "Exists" if match else "No Match in List B")
但是,由于您并不是真正在寻找子字符串,因此您应该在
中测试
,仅此而已:

for item in List_A:
     match = item in List_B
     print "%10s %s" % (item, "Exists" if match else "No Match in List B")
但您可以简单地使用集合差分:

print set(List_A) - set(List_B)

您可以将
List_B
转换为一个集合,因为集合提供了O(1)查找:

但请注意,集合只允许存储可散列(不可变)项。如果
List\u B
包含可变项,则首先将它们转换为不可变项,如果不可能,则排序
List\u B
并使用模块对排序的
List\u B

List_A = ["test_1", "test_2", "test_3", "test_4", "test_5"]
List_B = ["test_1", "test_2", "test_3", "test_4"]
s = set(List_B)
for item in List_A:
    match = item in s  # check if item is in set s
    print "%10s %s" % (item, "Exists" if match else "No Match in List B")
输出:

test_1 Exists
test_2 Exists
test_3 Exists
test_4 Exists
test_5 No Match in List B
test_1 Exists
test_2 Exists
test_3 Exists
test_4 Exists
test_5 No Match in List B

您可以将
List_B
转换为一个集合,因为集合提供了O(1)查找:

但请注意,集合只允许存储可散列(不可变)项。如果
List_B
包含可变项,则首先将其转换为不可变项,如果不可能,则对
List_B
进行排序,并使用模块对排序后的
List_B
进行二进制搜索

List_A = ["test_1", "test_2", "test_3", "test_4", "test_5"]
List_B = ["test_1", "test_2", "test_3", "test_4"]
s = set(List_B)
for item in List_A:
    match = item in s  # check if item is in set s
    print "%10s %s" % (item, "Exists" if match else "No Match in List B")
输出:

test_1 Exists
test_2 Exists
test_3 Exists
test_4 Exists
test_5 No Match in List B
test_1 Exists
test_2 Exists
test_3 Exists
test_4 Exists
test_5 No Match in List B

只需使用一个简单的for循环:

List_A = ["test_1", "test_2", "test_3", "test_4", "test_5"]
List_B = ["test_1", "test_2", "test_3", "test_4"]
for i in List_A:
    print "{:>10} {}".format(i, "Exists" if i in List_B else "No Match in List B")

只需使用一个简单的for循环:

List_A = ["test_1", "test_2", "test_3", "test_4", "test_5"]
List_B = ["test_1", "test_2", "test_3", "test_4"]
for i in List_A:
    print "{:>10} {}".format(i, "Exists" if i in List_B else "No Match in List B")
将输出

将输出