python-3.x - python 列表中的冲突案例

标签 python-3.x nested-lists

我有两个集合列表 -

属性 = [{0, 1, 2, 3, 6, 7}, {4, 5}]

决定 = [{0, 1, 2}, {3, 4}, {5}, {6, 7}]

我要-

{3, 4}

这里,{3, 4} 是冲突的,因为它既不是 {0, 1, 2, 3, 6, 7} 的子集,也不是{4, 5}

我的代码-

 check = []
 for i in attribute:
      for j in decision:
          if j.issubset(i):
              check.append(j)
 print(check)

 for x in decision:
     if not x in check:
         temp = x

 print(temp)

这给了我 {3, 4},但是有没有更简单(和/或)更快的方法来做到这一点?

最佳答案

您可以使用以下列表理解:

[d for d in decision if not any(d <= a for a in attribute)]

返回:

[{3, 4}]

如果您只想要第一个满足条件的集合,您可以使用带有生成器表达式的next:

next(d for d in decision if not any(d <= a for a in attribute))

返回:

{3, 4}

关于python-3.x - python 列表中的冲突案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53272008/

相关文章:

python-3.x - Python 程序给了我错误的答案

python - 无法打开数据库文件 - Mayan EDMS

python - 如何复制在 Python 函数调用中不会更改的列表

python - 从嵌套列表中删除重复项

css - 在悬停时在 CSS 中定位嵌套列表

list - 学习箱形图和指针图的资源

python - 解析 JSON 时处理 KeyError

python - PyQ 中的 Kdb 数据库到 NumPy 数组

python - 将列表中的元素替换为包含所述元素的嵌套列表(python3)

Python 约定为自动化目的强制变量前缀