python-2.7 - 如何在嵌套循环中获取值[条件]
问题描述
我是 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)
有没有办法可以简化代码?
解决方案
推荐阅读
- postgresql - 无法通过子查询按顺序使用数学 - 列不存在
- google-app-engine - 如何从 Google 的标准应用引擎 (Python 3.7) 连接到 Redis 实例 (memorystore)
- caldav - 无法从苹果 caldav 服务器获取用户的“当前用户主体”
- amazon-web-services - 如何使用秘密管理器存储数据库密码?
- r - 循环遍历子列表,其中“子子”列表保持不变
- angular - 我如何在角度中使用品牌 fontawesome 图标
- javascript - 嵌套对象数组的 Joi 错误消息
- ruby-on-rails - 如何将 simple_form 与嵌套的 ActiveModel 对象(不是 ActiveRecord)一起使用?
- php - Hashtags:只创建一种关系
- reactjs - 如何解决不存在“Access-Control-Allow-Origin”标头的问题