Python 3.x Python中dictionary的from_keys()方法的奇怪行为

Python 3.x Python中dictionary的from_keys()方法的奇怪行为,python-3.x,dictionary,Python 3.x,Dictionary,我已经创建了一个列表,例如: abc=[1,2,3,4,5,6] 现在我正在创建两个空字典,其中键是no,值是空列表 aa=[i for i in range(0,30,6)] diction=dict.fromkeys(aa,[]) 另一本词典是: dict_test={i:[] for i in range(0,30,6)} 这两本词典是: diction is {0:[],6:[],12:[],18:[],24:[]} dict_test is {0:[],6:[],12:[],18:

我已经创建了一个列表,例如:

abc=[1,2,3,4,5,6]

现在我正在创建两个空字典,其中键是no,值是空列表

aa=[i for i in range(0,30,6)]
diction=dict.fromkeys(aa,[])
另一本词典是:

dict_test={i:[] for i in range(0,30,6)}
这两本词典是:

diction is {0:[],6:[],12:[],18:[],24:[]}
dict_test is {0:[],6:[],12:[],18:[],24:[]}
这里,
dict\u test==发音为True

现在我只想将值附加到一个键上,因此,我使用了一个简单的for循环:

for i in abc:
    diction[0].append([i])
    dict_test[0].append([i])
但奇怪的行为是:

措辞变成:

{0: [[1], [2], [3], [4], [5], [6]],
 6: [[1], [2], [3], [4], [5], [6]],
 12: [[1], [2], [3], [4], [5], [6]],
 18: [[1], [2], [3], [4], [5], [6]],
 24: [[1], [2], [3], [4], [5], [6]]}
{0: [[1], [2], [3], [4], [5], [6]], 6: [], 12: [], 18: [], 24: []}
口述测验变成:

{0: [[1], [2], [3], [4], [5], [6]],
 6: [[1], [2], [3], [4], [5], [6]],
 12: [[1], [2], [3], [4], [5], [6]],
 18: [[1], [2], [3], [4], [5], [6]],
 24: [[1], [2], [3], [4], [5], [6]]}
{0: [[1], [2], [3], [4], [5], [6]], 6: [], 12: [], 18: [], 24: []}

dict_测试给出正确的输出,但diction正在更新所有键。请提供对这种行为的见解,因为它非常奇怪。

或。来自_keys的
的第二个参数可以是任何对象。函数如何知道如何从中生成独立克隆?因此,所有值都是对所述对象的引用。对不起。我看不出来,因为两者都是字典。在比较和类型上,它给出了字典作为类型,但两者在结构上有什么区别吗。为什么在只显式更新一个键的情况下更新所有键。在一个字典中,所有值都是对同一个空列表的引用。另一个的值都是不同的空列表。谢谢。我正在删除我的问题。我得到了解决方案的参考。无需删除。副本收集流量并指向答案