首页 > 解决方案 > 如何在嵌套循环中获取值[条件]

问题描述

我是 python 的初学者。我有两个列表,我必须从第一个列表中获取不匹配的列表。例如。

list_one= ['a', 'b', 'c', 'd']
list_two = ['d', 'c', 'b']

我的输出必须是 ['a']

现在,我正在使用带有标志的嵌套循环,该标志表明该元素是否存在。我在想可能有比这更干净的方法。

for doc in list1:
  item_exist = False
  for doc2 in list:
    if doc.lower() == doc2.lower():
      item_exist = True
      break
  if not item_exist:
    result.append(doc1)

有没有办法可以简化代码?

标签: python-2.7refactoringconditional

解决方案


推荐阅读