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 - Fatal编程技术网

Python 如何检查列表项是否存在于其他列表中

Python 如何检查列表项是否存在于其他列表中,python,list,Python,List,我试图比较两个列表(预期和实际)。我想检查实际列表项中是否存在预期的列表项。我正在尝试下面的示例代码。我可以尝试设置(预期)-设置(实际)。这会给我带来差异,但我想检查项目是否存在,或者显示哪个项目不存在。有人可以指导我,如何达到低于预期的结果,或者我犯了什么错误。如果有错误,请忽略,因为我是一名学习者 actual = ['resources.sh', 'server.properties', 'resources.csv', 'resources.log', 'sampleresources

我试图比较两个列表(预期和实际)。我想检查实际列表项中是否存在预期的列表项。我正在尝试下面的示例代码。我可以尝试
设置(预期)-设置(实际)
。这会给我带来差异,但我想检查项目是否存在,或者显示哪个项目不存在。有人可以指导我,如何达到低于预期的结果,或者我犯了什么错误。如果有错误,请忽略,因为我是一名学习者

actual = ['resources.sh', 'server.properties', 'resources.csv', 'resources.log', 'sampleresources.csv']
    expected = ['resources.sh', 'server.properties', 'resources.csv', 'resources.log']
    for i in expected:
        for b in actual:
            if b.split(".")[0] in i:
                print "{} is present".format(b)
            else:
                print "{} is not present".format(i)
实际结果:

resources.sh is present
resources.sh is not present
resources.csv is present
resources.log is present
resources.sh is not present
server.properties is not present
server.properties is present
server.properties is not present
server.properties is not present
server.properties is not present
resources.sh is present
resources.csv is not present
resources.csv is present
resources.log is present
resources.csv is not present
resources.sh is present
resources.log is not present
resources.csv is present
resources.log is present
resources.log is not present
预期结果:

resources.sh is present
server.properties is present
resources.csv is present 
resources.log is present 
sampleresources.csv is not present

您只需在
actual
中循环一次:

for i in actual:
     if i in expected:
         print(i, "is present")
     else:
         print(i, "is not present")
输出:

resources.sh is present
server.properties is present
resources.csv is present
resources.log is present
sampleresources.csv is not present
resources.sh is present
server.properties is present
resources.csv is present
resources.log is present
sampleresources.csv is not present
输出:


您可以使用列表理解来获得更清晰的代码:

  actual = ['resources.sh', 'server.properties', 'resources.csv', 'resources.log', 'sampleresources.csv']
  expected = ['resources.sh', 'server.properties', 'resources.csv', 'resources.log']

  def print_msg(x):
      print(x,'is present')

  [print_msg(b) for i in actual for b in expected if i == b]

您是在寻找设置交叉路口吗?谢谢@JonClements。这是正确的方法吗<代码>如果len(设置(预期).交叉点(设置(实际))>0:打印为真否则:打印为假感谢您的回答我很高兴能提供帮助。
[print ("{} is present".format(b)) if b in expected  
else print("{} is not present".format(b)) for b in actual]
actual = ['resources.sh', 'server.properties', 'resources.csv', 'resources.log', 'sampleresources.csv']
expected = ['resources.sh', 'server.properties', 'resources.csv', 'resources.log']

result = [elem + ' is present' if elem in expected else elem + ' is not present' for elem in actual]
print result
  actual = ['resources.sh', 'server.properties', 'resources.csv', 'resources.log', 'sampleresources.csv']
  expected = ['resources.sh', 'server.properties', 'resources.csv', 'resources.log']

  def print_msg(x):
      print(x,'is present')

  [print_msg(b) for i in actual for b in expected if i == b]