我有两个如下变量
k1 = [[0, 1, 1], [0, 0, 0]]
k2 = [[1, 0, 0], [0, 0, 1], [1, 1, 0], [0, 1, 1]]
我想删除每个列表中的[0,1,1]。 因此结果将如下所示。
k1 = [[0, 0, 0]]
k2 = [[1, 0, 0], [0, 0, 1], [1, 1, 0]]
答案 0 :(得分:1)
搜索交集并进行理解:
>>> k2 = [[1, 0, 0], [0, 0, 1], [1, 1, 0], [0, 1, 1]]
>>> k1 = [[0, 1, 1], [0, 0, 0]]
>>> intersection = [x for x in k2 if x in k1]
>>> intersection
[[0, 1, 1]]
>>> k2 = [x for x in k2 if x not in intersection]
>>> k2
[[1, 0, 0], [0, 0, 1], [1, 1, 0]]
>>> k1 = [x for x in k1 if x not in intersection]
>>> k1
[[0, 0, 0]]