简体   繁体   English

在python中更新输入字典

[英]updating an input dictionary in python

I am defining a function 我正在定义一个功能

def update(dictionary,key,value):
    dictionary[key] = value
   #  if i do print dictionary... it still shows the original value?

I want to update the input dictionary in the main call 我想在主通话中更新输入字典

So this is the main function i wrote: 所以这是我写的主要功能:

def update_mapping(mapping_dict,check_word,solution_word):
    #print "here "
    #new_mapping_dict = {}
    for i,ele in enumerate(check_word):
        if mapping_dict.has_key(ele):
            if mapping_dict[ele] == "*":
                mapping_dict[ele] = solution_word[i]

                print ele, solution_word
    print mapping_dict,check_word,solution_word

Basically I am inputting a misspelled word and then misspelled word has some mapping.. I do that mapping in the dictionary.. Like 基本上,我输入的是拼写错误的单词,然后拼写错误的单词具有一些映射..我在字典中进行了映射。

 mapping_dict ={"a":"x"...."s":"*"...}

So all the known alphabets whose mapping have been found have a legit key value alphabet pairs.. for the alphabets for which I havent found the right mapping, I am replacing them with "*" and I am finding them with some algorithm (inverted index) 因此,所有已找到映射的已知字母都有一个合法的键值字母对。.对于我没有找到正确映射的字母,我将其替换为“ *”,并使用某种算法(倒排索引)来查找它们)

And as i found those, I want to update my dictionary? 当我找到那些字典时,我想更新字典吗?

>>> test = {"test": 1}
>>> def update(dictionary,key,value):
...     dictionary[key] = value
... 
>>> update(test, "test", 2)
>>> test
{'test': 2}

Your problem must be elsewhere - this works. 您的问题必须在其他地方-这可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM