Python 如何重置字典中的所有值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22991888/
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 02:06:28  来源:igfitidea点击:

How to reset all values in a dictionary

pythondictionary

提问by user3112327

{"green": 0, "y3": 1, "m@tt": 0, "newaccount": 0, "egg": 0, "results": 0, "dan": 0, "Lewis": 0, "NewAccount2": 0, "testyear3": 1, "testyear6": 0, "NewAccount1": 0, "testyear4": 0, "testyear5": 0, "Matt1": 0, "swag": 1, "lewis": 1, "matt": 1, "notin": 0}

this is the dictionary defined as 'completeddict'. What I want to do, is to change ALL values no matter what they are called to 0. However bear in mind that new account names will be added at any point as 'keys' so I cannot manually do "completeddict[green] = 0", "completeddict[lewis] = 0", etc etc.

这是定义为“completeddict”的字典。我想要做的是将所有值更改为 0,无论它们被称为什么。但是请记住,新帐户名称将随时添加为“键”,因此我无法手动执行“completeddict[green] = 0 ", "completeddict[lewis] = 0" 等等。

Is there any way to have python change ALL values within a dictionary back to 0?

有没有办法让python将字典中的所有值都改回0?

EDIT: Unfortunately I do not want to create a new dictionary - I want to keep it called 'completeddict' as the program needs to use the dictionary defined as 'completeddict' at many points in the program.

编辑:不幸的是,我不想创建一个新字典 - 我想将它保留为“completeddict”,因为程序需要在程序中的许多点使用定义为“completeddict”的字典。

回答by wnnmaw

Sure, its pretty easy:

当然,这很简单:

newDict = {key: 0 for key in completeddict}

This is dictionary comprehension and is equivalent to:

这是字典理解,相当于:

newDict = {}
for key in completeddict:
    newDict[key] = 0

These dict comprehensions were introduced in v2.7, so older vesions will need to use the expanded form.

这些 dict 推导式是在 v2.7 中引入的,因此较旧的版本将需要使用扩展形式。

回答by alecxe

Another option is to use .fromkeys():

另一种选择是使用.fromkeys()

fromkeys(seq[, value])

Create a new dictionary with keys from seq and values set to value.

fromkeys(seq[, value])

使用 seq 中的键和设置为 value 的值创建一个新字典。

d = d.fromkeys(d, 0)

Demo:

演示:

>>> d = {"green": 0, "y3": 1, "m@tt": 0, "newaccount": 0, "egg": 0, "results": 0, "dan": 0, "Lewis": 0, "NewAccount2": 0, "testyear3": 1, "testyear6": 0, "NewAccount1": 0, "testyear4": 0, "testyear5": 0, "Matt1": 0, "swag": 1, "lewis": 1, "matt": 1, "notin": 0}
>>> d.fromkeys(d, 0)
{'newaccount': 0, 'swag': 0, 'notin': 0, 'NewAccount1': 0, 'Matt1': 0, 'testyear4': 0, 'Lewis': 0, 'dan': 0, 'matt': 0, 'results': 0, 'm@tt': 0, 'green': 0, 'testyear5': 0, 'lewis': 0, 'NewAccount2': 0, 'y3': 0, 'testyear3': 0, 'egg': 0, 'testyear6': 0}

回答by dawg

A form that will work in virtually any Python version is just use the dictconstructor with a list comprehension or generator:

几乎可以在任何 Python 版本中使用的表单只需使用dict带有列表推导式或生成器的构造函数:

>>> d={"green": 10, "y3": 11, "m@tt": 12, "newaccount": 22, "egg": 55, "results": 0, "dan": 0, "Lewis": 0, "NewAccount2": 0, "testyear3": 1, "testyear6": 0, "NewAccount1": 0, "testyear4": 0, "testyear5": 0, "Matt1": 0, "swag": 1, "lewis": 1, "matt": 1, "notin": 0}
>>> d=dict((k, 0) for k in d)
>>> d
{'newaccount': 0, 'swag': 0, 'notin': 0, 'NewAccount1': 0, 'Matt1': 0, 'testyear4': 0, 'Lewis': 0, 'dan': 0, 'matt': 0, 'results': 0, 'm@tt': 0, 'green': 0, 'testyear5': 0, 'lewis': 0, 'NewAccount2': 0, 'y3': 0, 'testyear3': 0, 'egg': 0, 'testyear6': 0}

Which you can then use with a mutable argument or some form of callable:

然后您可以将其与可变参数或某种形式的可调用参数一起使用:

>>> d=dict((k, list()) for k in d)

Now each element of dis a new list (or whatever else mutable or not you would want to set the value portion of the tuple to be).

现在的每个元素d都是一个新列表(或者任何其他可变的或不可变的,您希望将元组的值部分设置为)。

回答by Bryan Oakley

If you don't want to create a new dict, it seems like all you need is a simple two-line loop, unless I'm missing something:

如果你不想创建一个新的 dict,你只需要一个简单的两行循环,除非我遗漏了一些东西:

for key in completeddict.keys():
    completeddict[key] = 0

回答by surendra

Adding to alecxeasnwer,

添加到alecxeasnwer,

your dictionary is,

你的字典是

completeddict = {"green": 0, "y3": 1, "m@tt": 0, "newaccount": 0, "egg": 0, "results": 0, "dan": 0, "Lewis": 0, "NewAccount2": 0, "testyear3": 1, "testyear6": 0, "NewAccount1": 0, "testyear4": 0, "testyear5": 0, "Matt1": 0, "swag": 1, "lewis": 1, "matt": 1, "notin": 0}

now this dictionary can be updated with default values,

现在这个字典可以用默认值更新,

completeddict.update({}.fromkeys(completeddict,0)

Done. your completeddict is updated with default value for all keys.

完毕。您的 Completeddict 已更新为所有键的默认值。

Ex:

前任:

a = {'x':2,'y':4}
print id(a)  
print a
a.update({}.fromkeys(a,0))
print id(a)
print a

Output:

输出:

42612608     <--- id of dict 'a'
{'y': 4, 'x': 2}     <--- dict 'a' before updation
42612608     <--- id of dict 'a'   
{'y': 0, 'x': 0}     <--- dict 'a' after updation

It can be observed that id of the dictionary same even after it is updated with default values.

可以观察到,即使使用默认值更新字典的 id 也是相同的。