Python 字典替换值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19773669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:34:10  来源:igfitidea点击:

Python dictionary replace values

pythonpython-3.xdictionary

提问by user1478335

I have a dictionary with 20 000 plus entries with at the moment simply the unique word and the number of times the word was used in the source text (Dante's Divine Comedy in Italian).

我有一本字典,其中包含 20 000 多个条目,目前只有唯一的单词和该单词在源文本中使用的次数(但丁的意大利语《神曲》)。

I would like to work through all entries replacing the value with an actual definition as I find them. Is there a simple way to iterate through the keywords that have as a value a number in order to replace (as I research the meaning)?

我想处理所有条目,当我找到它们时,用实际定义替换值。是否有一种简单的方法来迭代具有数字作为值的关键字以替换(当我研究含义时)?

The dictionary starts:

字典开头:

{'corse': 378, 'cielo,': 209, 'mute;': 16, 'torre,': 11, 'corsa': 53, 'assessin': 21, 'corso': 417, 'Tolomea': 21}  # etc.

Sort of an application that will suggest a keyword to research and define.

一种应用程序,它将建议一个关键字进行研究和定义。

回答by Martijn Pieters

You cannot select on specific values (or types of values). You'd either make a reverse index (map numbers back to (lists of) keys) or you have to loop through allvalues every time.

您不能选择特定值(或值类型)。您要么创建一个反向索引(将数字映射回(列表)键),要么每次都必须遍历所有值。

If you are processing numbers in arbitrary order anyway, you may as well loop through all items:

如果您以任意顺序处理数字,您也可以遍历所有项目:

for key, value in inputdict.items():
    # do something with value
    inputdict[key] = newvalue

otherwise I'd go with the reverse index:

否则我会使用反向索引:

from collections import defaultdict

reverse = defaultdict(list)
for key, value in inputdict.items():
    reverse[value].append(key)

Now you can look up keys by value:

现在您可以按值查找键:

for key in reverse[value]:
    inputdict[key] = newvalue

回答by Andrew Clark

If you iterate over a dictionary you get the keys, so assuming your dictionary is in a variable called dataand you have some function find_definition()which gets the definition, you can do something like the following:

如果您遍历字典,您将获得键,因此假设您的字典位于一个被调用的变量中,data并且您有一些find_definition()获取定义的函数,您可以执行以下操作:

for word in data:
    data[word] = find_definition(word)

回答by Rotareti

via dict.update() function

通过 dict.update() 函数

In case you need a declarativesolution, you can use dict.update()to change values in a dict.

如果您需要声明性解决方案,您可以使用dict.update()来更改 dict 中的值。

Either like this:

要么像这样:

my_dict.update({'key1': 'value1', 'key2': 'value2'})

or like this:

或者像这样:

my_dict.update(key1='value1', key2='value2')

via dictionary unpacking

通过字典解包

Since Python 3.5you can also use dictionary unpackingfor this:

Python 3.5 开始,您还可以为此使用字典解包

my_dict = { **my_dict, 'key1': 'value1', 'key2': 'value2'}

Note: This creates a new dictionary.

注意:这会创建一个新字典。

via merge operator or update operator

通过合并运算符或更新运算符

Since Python 3.9you can also use the merge operatoron dictionaries:

Python 3.9 开始,您还可以在字典上使用合并运算符

my_dict = my_dict | {'key1': 'value1', 'key2': 'value2'}

Note: This creates a new dictionary.

注意:这会创建一个新字典。

Or you can use the update operator:

或者您可以使用更新运算符

my_dict |= {'key1': 'value1', 'key2': 'value2'}