python - 获取 2 个列表之间不常见的项目

标签 python python-3.x list set-difference

我有 2 个列表 'a1''a2':

a1 = [[1, 4], [1, 10], [2, 5], [2, 11], [3, 6], [4, 7], [4, 12], [5, 8], [5, 13], 
      [6, 9], [7, 14], [8, 15], [2, 10], [3, 11], [5, 12], [6, 13], [8, 14], [9, 15]]

a2 = [[1, 10], [2, 11], [4, 12], [5, 13], [7, 14], [8, 15], [2, 10], [3, 11], [5, 12], 
      [6, 13], [8, 14], [9, 15]]

我正在创建第三个列表,其中包含前两个列表之间不常见的元素,我应该有这样的东西。

[[1, 4], [2, 5], [3, 6], [4, 7], [5, 8], [6, 9]]

可以应用集合差异来提取第一个列表中不包含第二个列表的元素。

a3 = a1.difference(a2)

问题是它适用于 2 个列表,而不是像本例那样适用于 2 个列表列表。

针对此类情况有有效的解决方案吗?谨致问候。

最佳答案

[x for x in a1 if x not in a2]

关于python - 获取 2 个列表之间不常见的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61662803/

相关文章:

python - 使用 spark ml(数据框)进行逻辑回归

python - 将 csv edgelist 读入 networkx

python - 在映射函数的列表推导式中传递命名参数

Python:遍历列表,将空白输出到第二个列表,然后按顺序调用

python - 在Python中,有没有一种方法可以使用.format表示法将列表打印到字符串中?

python - 在 list1 中查找也在 list2 中的项目并删除不在 list1 中的项目

python:导入 libpci 引发 SyntaxError

python - 类型错误 : object() takes no parameters after defining __new__

python - 将数据帧写入文件时如何进行排序?

python - 在生成器上应用 numpy 函数 (np.vstack) 返回 2 个值