不确定为什么字典是变异的

0 python dictionary mutated

通过麻省理工学院开放课件进行Python的自学,并遇到下面这段代码的问题.当我单独或在另一个函数中运行此函数时,它会改变最初传递的值'hand',我不知道为什么.我设置了两个局部变量(hand0和tester),第一个保留初始值,第二个迭代结束.然而,所有三个都改变了,而我只是期望'测试者'这样做.除了改变'hand'之外,该函数按预期工作.

(传递给函数的值在set参数中有所不同:word_list是有效英文单词列表,word是我在此函数中替换的字符串,用于测试,hand是字母及其相关计数字典.调试代码注释掉.)

def is_valid_word(word, hand, word_list):
    """
    Returns True if word is in the word_list and is entirely
    composed of letters in the hand. Otherwise, returns False.
    Does not mutate hand or word_list.

    word: string
    hand: dictionary (string -> int)
    word_list: list of lowercase strings
    """
    hand0 = hand
    tester = hand
    #display_hand(hand)
    #display_hand(tester)
    word = raw_input('test word: ')
    length = len(word)
    disc = True
    for c in range(length):
        if word[c] in tester.keys() and tester[word[c]]>0:
            #print tester[word[c]]
            #display_hand(hand)
            #display_hand(tester)
            tester[word[c]]=tester[word[c]]-1            
        else:
            #print 'nope'
            disc = False
    if word not in word_list:
        disc = False
    #print disc
    #display_hand(hand)
    #display_hand(tester)
    #display_hand(hand0)
    return disc
Run Code Online (Sandbox Code Playgroud)

mgi*_*son 9

当你这样做时tester = hand,你只是创建一个hand对象的新引用.换句话说,testerhand相同的对象.如果您检查了id以下内容,您可以看到:

print id(tester)
print id(hand)  #should be the same as `id(tester)`
Run Code Online (Sandbox Code Playgroud)

或者等效地,与is运营商进行比较:

print tester is hand  #should return `True`
Run Code Online (Sandbox Code Playgroud)

要制作字典的副本,有一种.copy方法可用:

tester = hand.copy()
Run Code Online (Sandbox Code Playgroud)


Mar*_*som 5

当您这样做时,您tester = hand并不是在复制hand,而是对同一对象进行新的引用。您所做的任何修改都tester将反映在 中hand

用于tester = hand.copy()解决此问题:http ://docs.python.org/2/library/stdtypes.html#dict.copy