【发布时间】:2014-09-07 09:41:32
【问题描述】:
我正在尝试将一个元素添加到 Python 字典中的集合中,但是当我使用 mydictionary[index].update([newelement]) 时,所有字典都将使用新元素进行更新。这里是 python 控制台命令和输出来演示我在说什么:
>>> graph=dict.fromkeys(range(10),set([]))
>>> graph
{0: set([]), 1: set([]), 2: set([]), 3: set([]), 4: set([]), 5: set([]), 6: set([]), 7: set([]), 8: set([]), 9: set([])}
>>> graph[0].update([1])
>>> graph
{0: set([1]), 1: set([1]), 2: set([1]), 3: set([1]), 4: set([1]), 5: set([1]), 6: set([1]), 7: set([1]), 8: set([1]), 9: set([1])}
那么为什么不只更新graph[0] 的条目呢?
我真的很想专门找一个有这个问题的话题,但是没有找到。
Obs:带数字的括号是python控制台中的命令。
【问题讨论】:
标签: python dictionary set