删除包含子列表的列表
Removing Lists that Contain Sublists
有没有办法从包含子列表的列表中删除列表?
假设我有 5 个元素,从 a 到 e。
我在下面找到了从尺码 0 到尺码 5 的所有组合:
all_combinations = [[], ['a'], ['b'], ['c'], ['d'], ['e'], ['a', 'b'], ['a', 'c'], ['a', 'd'], ['a',
'e'], ['b', 'c'], ['b', 'd'], ['b', 'e'], ['c', 'd'], ['c', 'e'], ['d', 'e'],
['a', 'b', 'c'], ['a', 'b', 'd'], ['a', 'b', 'e'], ['a', 'c', 'd'], ['a', 'c',
'e'], ['a', 'd', 'e'], ['b', 'c', 'd'], ['b', 'c', 'e'], ['b', 'd', 'e'],
['c', 'd', 'e'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'e'], ['a', 'b',
'd', 'e'], ['a', 'c', 'd', 'e'], ['b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']]
现在假设我要删除其中一些包含子列表的组合:
sublists = [['a', 'c'], ['c', 'd'], ['b', 'e']]
有没有简单的方法可以做到这一点?我应该只剩下不包含这些子列表的组合。我应该只得到 a 和 c 不在一起,c 和 d 不在一起,b 和 e 不在一起的列表。
编辑:想要得到这样的输出:
valid_combinations = [[], ['a'], ['b'], ['c'], ['d'], ['e'], ['a', 'b'],
['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'],
['c', 'e'], ['d', 'e'], ['a', 'b', 'd'],
['a', 'd', 'e']]
试试这个:
result = [item for item in all_combinations if item not in sublists]
一种可能的方法是使用集合,特别是函数 issuperset
来检查一个列表是否包含另一个列表的所有元素。因此,以下列表理解 returns all_combinations
的所有元素 a
不包含 sublists
中任何 b
的每个元素
[a for a in all_combinations if all(not set(a).issuperset(set(b)) for b in sublists)]
您可以使用 sets 查看 sublists
中的完整项目列表是否包含在 all_combinations
的子列表中:
>>> [sl for sl in all_combinations if not any(set(e)<=set(sl) for e in sublists)]
[[], ['a'], ['b'], ['c'], ['d'], ['e'], ['a', 'b'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'], ['c', 'e'], ['d', 'e'], ['a', 'b', 'd'], ['a', 'd', 'e']]
>>> _==valid_combinations
True
有没有办法从包含子列表的列表中删除列表?
假设我有 5 个元素,从 a 到 e。
我在下面找到了从尺码 0 到尺码 5 的所有组合:
all_combinations = [[], ['a'], ['b'], ['c'], ['d'], ['e'], ['a', 'b'], ['a', 'c'], ['a', 'd'], ['a',
'e'], ['b', 'c'], ['b', 'd'], ['b', 'e'], ['c', 'd'], ['c', 'e'], ['d', 'e'],
['a', 'b', 'c'], ['a', 'b', 'd'], ['a', 'b', 'e'], ['a', 'c', 'd'], ['a', 'c',
'e'], ['a', 'd', 'e'], ['b', 'c', 'd'], ['b', 'c', 'e'], ['b', 'd', 'e'],
['c', 'd', 'e'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'e'], ['a', 'b',
'd', 'e'], ['a', 'c', 'd', 'e'], ['b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']]
现在假设我要删除其中一些包含子列表的组合:
sublists = [['a', 'c'], ['c', 'd'], ['b', 'e']]
有没有简单的方法可以做到这一点?我应该只剩下不包含这些子列表的组合。我应该只得到 a 和 c 不在一起,c 和 d 不在一起,b 和 e 不在一起的列表。
编辑:想要得到这样的输出:
valid_combinations = [[], ['a'], ['b'], ['c'], ['d'], ['e'], ['a', 'b'],
['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'],
['c', 'e'], ['d', 'e'], ['a', 'b', 'd'],
['a', 'd', 'e']]
试试这个:
result = [item for item in all_combinations if item not in sublists]
一种可能的方法是使用集合,特别是函数 issuperset
来检查一个列表是否包含另一个列表的所有元素。因此,以下列表理解 returns all_combinations
的所有元素 a
不包含 sublists
b
的每个元素
[a for a in all_combinations if all(not set(a).issuperset(set(b)) for b in sublists)]
您可以使用 sets 查看 sublists
中的完整项目列表是否包含在 all_combinations
的子列表中:
>>> [sl for sl in all_combinations if not any(set(e)<=set(sl) for e in sublists)]
[[], ['a'], ['b'], ['c'], ['d'], ['e'], ['a', 'b'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'], ['c', 'e'], ['d', 'e'], ['a', 'b', 'd'], ['a', 'd', 'e']]
>>> _==valid_combinations
True