【问题标题】:pythonic way to check if any elements of a list is present in a setpythonic方法来检查一个列表的任何元素是否存在于集合中
【发布时间】:2021-05-05 13:04:54
【问题描述】:

试图找到一种方法来检查以下内容:

approved_countries = ['Germany', 'France']

我们有 5 套:

{'Germany'}
{'Germany', 'France'}
{'Germany', 'France'}
{'Germany'}
{'Germany', 'Italy'}

我需要遍历 sets 并标记出国家/地区不在 list 已批准国家/地区的集合。

所以在这种情况下,我需要标记包含意大利的集合。

实现这一目标的最 Pythonic 方式是什么?

【问题讨论】:

  • 请指定您是否需要针对 dtaframe 或您的集合的解决方案,因为这可能会改变解决问题的方式。如果是数据框,请提供示例数据框
  • @Andreas 是有道理的。我已经澄清了帖子。感谢您的反馈。

标签: python list set list-comprehension


【解决方案1】:
for index,row in enumerate(dataset):
    if any([i not in approved_countries for i in row)):
        print("Row {} is bad".format(index))

【讨论】:

  • 感谢 Tamir,这对我有帮助。
猜你喜欢
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 2015-03-20
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 2012-11-09
相关资源
最近更新 更多