Python:使用另一个大型词典更新大型词典 [英] Python: updating a large dictionary using another large dictionary
问题描述
我正在尝试使用另一个词典中具有相似键(相同日期但格式不同)的值来更新大型词典的某些值.我当前正在使用的进程太慢,我想减少瓶颈.
I am trying to update some values of a large dictionary using values from another dictionary where they have similar keys (the same date but in a different format). The process I'm currently using is too slow and I want to reduce the bottleneck.
这是我当前的解决方案(它将更新后的字典写到文件中):
This is my current solution (it writes the updated dict to a file):
from dateutil import parser
File = open(r'E:Test1.txt','w')
b = {'1946-1-1':0,..........,'2012-12-31':5}
d = {'1952-12-12':5,........,'1994-7-2':10}
for key1, val1 in b.items():
DateK1 = parser.parse(key1)
Value = val1
for key2, val2 in d.items():
DateK2 = parser.parse(key2)
if DateK1 == DateK2:
d[key2] = Value
Order= sorted(d.items(), key=lambda t: t[0])
for item in Order:
File.write('%s,%s\n' % item)
File.close()
推荐答案
您应使用 update
合并字典的方法:
You should use the update
method to merge dictionaries:
b.update(d)
.
此刻,您正在为b
中的每个键遍历d
...,这很慢.您可以通过设置两个具有匹配键的字典来解决此问题(相等的日期将对相同的哈希进行散列-也许这里要注意的很酷的事情是日期时间对象散列):
At the moment you are iterating over d
for every key in b
... which is slow. You can get around this by setting up two dictionaries which will have matching keys (and equal dates will hash the same - perhaps the cool thing to note here is that datetime objects hash):
b1 = dict( (parser.parse(k),v) for k,v for b.iteritems() )
d1 = dict( (parser.parse(k),v) for k,v for d.iteritems() )
d1.update(b1) # update d1 with the values from b1
我刚刚意识到,您并没有相当进行更新,因为仅更新了那些共享值,所以(同样,只需重复一次):
I've just realised that you're not quite doing an update, since only those shared values are being updated, so instead (again by just iterating once):
for k_d1 in d1:
if k_d1 in b1:
d1[k_d1] = b1[k_d1]
这篇关于Python:使用另一个大型词典更新大型词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!