Python 有没有一种快速方法可以用特定值更新嵌套字典中的一组键?
我有一个帮助字典,其中键是事件对和功能的嵌套元组,功能的数量可以在1-N之间。与事件对相关。该值是对所述事件对和特征的支持 我有一个字典Python 有没有一种快速方法可以用特定值更新嵌套字典中的一组键?,python,dictionary,Python,Dictionary,我有一个帮助字典,其中键是事件对和功能的嵌套元组,功能的数量可以在1-N之间。与事件对相关。该值是对所述事件对和特征的支持 我有一个字典d,这是一个嵌套字典,其中存储了对每个事件对的支持以及功能的每个可能的部分副本 这在下面的代码段中完成 help_d = {(('Event 1', 'Event 2'),('Feature A', 'Feature B',...,'Feature T', 'Feature H')) : 10, (('Event 1', 'Event
d
,这是一个嵌套字典,其中存储了对每个事件对的支持以及功能的每个可能的部分副本
这在下面的代码段中完成
help_d = {(('Event 1', 'Event 2'),('Feature A', 'Feature B',...,'Feature T', 'Feature H')) : 10,
(('Event 1', 'Event 3'),('Feature C', 'Feature E',...,'Feature H', 'Feature G')) : 50,
(('Event 1', 'Event 4'),('Feature F', 'Feature G',...,'Feature T', 'Feature X')) : 100,
.....
(('Event 10', 'Event 15'),('Feature D', 'Feature E',....,'Feature V', 'Feature B')) : 5}
d = defaultdict(int,defaultdict())
for key,value in help_d.items():
event_a = key[0][0]
event_b = key[0][1]
feature_tuple = key[1]
#Every possible partial duplicate of the features
all_keys_to_update = list(itertools.product(*zip(feature_tuple, itertools.repeat(''))))
#Nested for loop that takes around 3-4 secs per iteration
for key_to_update in all_keys_to_update:
d[(event_a,event_b)][key_to_update] += value
help\u dict
的大小约为12000个键
列表的大小all-keys-to-update
约为10000
嵌套for循环大约需要3-4秒来循环,这意味着完成此特定代码段大约需要11小时
示例,其中我只有3个事件和2个功能
这将导致以下字典d
d = {('Event 1','Event 2'): {('','') : 30,
('A','') : 30,
('','B') : 10,
('','C') : 20,
('A','B') : 10,
('A','C') : 20},
('Event 1','Event 3'): {('','') : 50,
('D','') : 50,
('','C') : 50,
('D','C') : 50},
('Event 2','Event 3'): {('','') : 10,
('D','') : 10,
('','B') : 10,
('D','B') : 10}}
是否有一种更快的方法来更新嵌套字典中具有相同值的一组键?通过减少索引的数量,您可以节省大约30%的时间(取决于数据),但鉴于生成的组合数量之多,我看不出您如何能够更快地完成:
d = defaultdict(lambda:defaultdict(int))
for (events,features),count in help_d.items():
counts = d[events]
for combo in product(*zip(features, repeat(''))):
counts[combo] += count
但是,根据您以后如何使用此词典,仅在使用时生成计数可能更有效。您可以通过创建一个类或函数来实现对给定事件和功能组合的“按需”计算
help_events = defaultdict(list) # list of feature patterns for each event pair
for (event,features),count in help_d.items():
help_events[event].append(features)
help_cache = dict() # cached results
def getFeatureCount(events,pattern):
# check cache first
if (events,pattern) in help_cache:
return help_cache[(events,pattern)]
# compute total of matching feature patterns
result = 0
for eventFeatures in help_events[events]:
if all(e==f or f=="" for e,f in zip(eventFeatures,pattern)):
result += help_d[(events,eventFeatures)]
#save to cache and return result
help_cache[(events,pattern)] = result
return result
用法:
getFeatureCount(('Event 1', 'Event 2'),('Feature A', '')) # --> 30
# wich is equivalent to d[(('Event 1', 'Event 2'),('Feature A', ''))]
你能编辑你的问题并把预期的结果放在那里吗?听起来你想要可变的键,这是可以做到的,但最好避免。您最好使用Python dict或Python defaultdict以外的东西。如果你真的认为你需要可变键,你可以尝试添加一个间接级别,比如让元组指向其他整体更新的单元素列表。@AndrejKesely我用一个简单的例子编辑过,请注意,事实上,我每个事件对将有3个以上的事件和2个以上的特性,但是不管怎样,逻辑都是一样的。@user1277476您能指定更多的细节吗?还有什么其他选择?
getFeatureCount(('Event 1', 'Event 2'),('Feature A', '')) # --> 30
# wich is equivalent to d[(('Event 1', 'Event 2'),('Feature A', ''))]