如果是x中的值,我需要删除y.value的值格式。
x = [[1,2],[2,6],[1,5],[5,6],[5,6],
[10,11], [11,12], [3,4],[4,8],[8,7],[3,7]]
y = {'first':[[1,2],[2,6],[1,5],[5,6]],
'second':[[2,3], [3,7],[7,6],[2,6]]}
for k,v in y.items():
for a in v:
if a in x:
del a
输出
{'first': [[1, 2], [2, 6], [1, 5], [5, 6]],
'second': [[2,3], [3, 7], [7, 6], [2, 6]]}
需要:
{'first': [],
'second': [[2, 3], [7, 6], [2, 6]]}
答案 0 :(得分:3)
创建set
个tuple
个您不想要的项目,然后就地更新您的词典并检查其转换为元组的内部列表项目是否也不在您的列表中,例如:
unwanted = {tuple(el) for el in x}
y.update((k, [el for el in v if tuple(el) not in unwanted]) for k, v in y.items())
这会留下y
:
{'first': [], 'second': [[2, 3], [7, 6]]}
答案 1 :(得分:1)
>>> x = [[1,2],[2,6],[1,5],[5,6],[5,6],
[10,11], [11,12], [3,4],[4,8],[8,7],[3,7]]
>>> y = {'first':[[1,2],[2,6],[1,5],[5,6]],
'second':[[2,3], [3,7],[7,6],[2,6]]}
>>> for k,v in y.items():
y[k] = [a for a in v if a not in x]
>>> y
{'second': [[2, 3], [7, 6]], 'first': []}
答案 2 :(得分:1)
使用remove
:
x = [[1,2],[2,6],[1,5],[5,6],[5,6],
[10,11], [11,12], [3,4],[4,8],[8,7],[3,7]]
y = {'first':[[1,2],[2,6],[1,5],[5,6]],
'second':[[2,3], [3,7],[7,6],[2,6]]}
for v in y.values():
for _x in x:
if _x in v:
v.remove(_x)
print(y) # {'first': [], 'second': [[2, 3], [7, 6]]}