亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

是否有一種快速方法可以使用特定值更新嵌套字典中的一組鍵?

是否有一種快速方法可以使用特定值更新嵌套字典中的一組鍵?

PHP
白板的微信 2023-11-09 21:17:54
我有一個幫助字典,其中鍵是事件對和特征的嵌套元組,特征的數量可以在 1 - N 之間。與事件對相關。該值是所述事件對和特征的支持。我有一個字典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_dict約為 12 000 個按鍵。該列表的大小all_keys_to_update約為 10 000。嵌套的 for 循環需要大約 3-4 秒的時間來循環,這意味著大約需要 11 個小時才能完成這個特定的代碼片段。我只有 3 個事件和 2 個功能的示例help_d = {(('Event 1', 'Event 2'),('Feature A', 'Feature B')) : 10,         (('Event 1', 'Event 2'),('Feature A', 'Feature C')) : 20,         (('Event 1', 'Event 3'),('Feature D', 'Feature C')) : 50,         (('Event 2', 'Event 3'),('Feature D', 'Feature B')) : 10}      是否有更快的方法來更新嵌套字典中具有相同值的一組鍵?
查看完整描述

1 回答

?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

通過減少索引數量,您可以節省大約 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', ''))] 


查看完整回答
反對 回復 2023-11-09
  • 1 回答
  • 0 關注
  • 150 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號