同时将字典的所有值替换为零python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13712229/
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
Simultaneously replacing all values of a dictionary to zero python
提问by xlharambe
I have a very large dictionary, maybe about 10,000 keys/valuesand I want to simultaneously change all values to 0. I am aware that I can loop through and set all the values to 0but it take forever. Is there anyway that I can simultaneouslyset allvalues to 0?
我有一个非常大的字典,可能大约10,000 keys/values并且我想同时将所有值更改为0. 我知道我可以循环并将所有值设置为,0但它需要永远。无论如何,我可以同时将所有值设置为0吗?
Looping method, very slow:
循环方法,很慢:
#example dictionary
a = {'a': 1, 'c': 1, 'b': 1, 'e': 1, 'd': 1, 'g': 1, 'f': 1, 'i': 1, 'h': 1, 'k': 1,
'j': 1, 'm': 1, 'l': 1, 'o': 1, 'n': 1, 'q': 1, 'p': 1, 's': 1, 'r': 1, 'u': 1,
't': 1, 'w': 1, 'v': 1, 'y': 1, 'x': 1, 'z': 1}
for key.value in a.items():
a[key] = 0
Output:
输出:
{'a': 0, 'c': 0, 'b': 0, 'e': 0, 'd': 0, 'g': 0, 'f': 0, 'i': 0, 'h': 0, 'k': 0,
'j': 0, 'm': 0, 'l': 0, 'o': 0, 'n': 0, 'q': 0, 'p': 0, 's': 0, 'r': 0, 'u': 0,
't': 0, 'w': 0, 'v': 0, 'y': 0, 'x': 0, 'z': 0}
采纳答案by Daniel Roseman
You want dict.fromkeys():
你想要dict.fromkeys():
a = dict.fromkeys(a, 0)
回答by Jonathan H
Thanks @akaRem for his comment :)
感谢@akaRem 的评论:)
a = dict.fromkeys( a.iterkeys(), 0 )
回答by Ioan Alexandru Cucu
If you know what type your dict values need to be, you could this:
如果您知道您的 dict 值需要是什么类型,您可以这样做:
- store the the dict values in an array.array object. This uses a continuous block of memory.
- the dict, rather than storing the actual values would store the array index at which the actual value can be found
- reinitialize the array with a contiguous binary string of zeros
- 将 dict 值存储在 array.array 对象中。这使用了一个连续的内存块。
- dict,而不是存储实际值将存储可以找到实际值的数组索引
- 使用连续的二进制零字符串重新初始化数组
Didn't test the performance, but it should be faster...
没有测试性能,但它应该更快......
import array
class FastResetDict(object):
def __init__(self, value_type):
self._key_to_index = {}
self._value_type = value_type
self._values = array.array(value_type)
def __getitem__(self, key):
return self._values[self._key_to_index[key]]
def __setitem__(self, key, value):
self._values.append(value)
self._key_to_index[key] = len(self._values) - 1
def reset_content_to_zero(self):
zero_string = '\x00' * self._values.itemsize * len(self._values)
self._values = array.array(self._value_type, zero_string)
fast_reset_dict = FastResetDict('i')
fast_reset_dict['a'] = 103
fast_reset_dict['b'] = -99
print fast_reset_dict['a'], fast_reset_dict['b']
fast_reset_dict.reset_content_to_zero()
print fast_reset_dict['a'], fast_reset_dict['b']
回答by Matthew Frost
Be warned, if the order of your keys matter the solution may not be suitable as it seems to reorder.
请注意,如果您的密钥顺序很重要,该解决方案可能不适合,因为它似乎重新排序。
To stop this from happening use list comprehension:
要阻止这种情况发生,请使用列表理解:
aDictionary = { x:0 for x in aDictionary}
Note:It's only 2.7.x and 2.x exclusive
注意:只有 2.7.x 和 2.x 独占
回答by Supamee
To expand on @Daniel Rosemananswer a=a.fromkeys(d,0)is functionaly the same and a bit faster. Also if you plan to do this frequently save=dict.fromkeys(a,0)and then call a=save.copy()which is faster in some cases(large dicts)
扩展@Daniel Roseman答案a=a.fromkeys(d,0)在功能上是相同的,而且速度要快一些。此外,如果您打算经常这样做save=dict.fromkeys(a,0),然后a=save.copy()在某些情况下调用哪个更快(大字典)

