1 回答

TA貢獻(xiàn)1785條經(jīng)驗 獲得超8個贊
通過減少索引數(shù)量,您可以節(jié)省大約 30% 的時間(取決于數(shù)據(jù)),但考慮到您生成的組合數(shù)量巨大,我不知道如何才能使速度更快:
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
但是,根據(jù)您之后如何使用該字典,僅在使用時生成計數(shù)可能會更有效。您可以通過創(chuàng)建一個類或函數(shù)來實現(xiàn)給定事件和功能組合的“按需”計算來實現(xiàn)這一點。
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', ''))]
- 1 回答
- 0 關(guān)注
- 165 瀏覽
添加回答
舉報