【发布时间】:2019-06-15 03:16:16
【问题描述】:
我创建了两个字典。每个都基于同一数据库的不同查询。每个字典中有一个来自数据库的键和四个字段。我想找到 dict_x 中的所有行都在 dict_y 中。
for row in dict_x:
if dict_y.values() not in dict_x.values():
del dict_x[row]
print 'Length dict_x', len(dict_x)
这会返回错误
TypeError: 'type' object does not support item deletion
【问题讨论】:
-
“行”是什么意思?字典没有行。它们有键和值。
-
另外,您将
dict_y.values()与dict_x.values()进行比较。这是在每次迭代中比较整个字典的值 - 肯定不是你想要的 -
“行”是指字典项——我使用“行”这个词是因为字典是由 SQL 查询形成的。
-
这里的迭代过程中字典大小不会改变还是我遗漏了什么
-
因此,如果
dict_x的键/行的值在dict_y中,那么您想从dict_x中删除key?
标签: python dictionary