我有两本字典,我想比较一下,看看两者有什么不同。我感到困惑的地方是dict. 有这个名字吗?
一切正常,我只是不明白它为什么有效或它在做什么。
x = {"#04": 0, "#05": 0, "#07": 0, "#08": 1, "#09": 0, "#10": 0, "#11": 1, "#12": 1, "#14": 1, "#15": 1, "#17": 0, "#18": 1, "#19": 1, "#20": 1}
y = {"#04": 1, "#05": 0, "#07": 0, "#08": 1, "#09": 0, "#10": 0, "#11": 1, "#12": 1, "#14": 1, "#15": 0, "#17": 1, "#18": 1, "#19": 0, "#20": 1}
dict = {k: x[k] for k in x if y[k] != x[k]}
list = []
for k, v in dict.items()
if v==0:
difference = k + ' became ' + '0'
list.append(difference)
else:
difference = k + ' became ' + '1'
list.append(difference)
print(list)
它应该打印,['#04 became 0', '#15 became 1', '#17 became 0', '#19 became 1']但我不明白dict循环遍历 x 和 y 字典的工作原理。
小怪兽爱吃肉
相关分类