May-03-2018, 06:45 AM
(This post was last modified: May-03-2018, 06:55 AM by Gribouillis.)
Is this way faster?
from collections import defaultdict struct = defaultdict(lambda: defaultdict(int)) struct['foo']['bar'] += 7 print(struct)The following version probably works for every version of python since 1.0
def addBytesToStatStruct(struct, subscriberId, protocolId, octents): try: d = struct[subscriberId] except KeyError: struct[subscriberId] = {protocolId: octents} return try: d[protocolId] += octents except KeyError: d[protocolId] = octentsOther solutions involve setdefault() and get() as buran said.