我有一个具体的问题,在这个问题中,我观察到python中所有的引用和取消引用的混淆。我有一个全局结构wordhistory
,我在函数addWordHistory
内的不同级别上进行了更改:
wordhistory = dict()
def addWordHistory(words):
global wordhistory
current = wordhistory
for word in words:
if current is None:
current = {word:[None,1]} #1
else:
if word in current:
current[word][1] += 1
else:
current[word] = [None,1]
current = current[word][0] #2
在#1
行中,我想更改已分配给#2
行中的局部变量current
的引用后面的值。这看起来不像这样。相反,我怀疑只有局部变量从对字典的引用更改。在
以下变体可以使用,但我想保存所有空假期字典的内存:
^{pr2}$
为了能够更改当前列表的项,您需要存储对列表的引用,而不仅仅是对需要更改的项的引用:
相关问题 更多 >
编程相关推荐