Optimizing dictionaries in Python -
my question different 1 asked here. asking improvements made code containing dictionaries. however, link explains memory profilers, next step.
i have following 2 sets of code achieve same thing.
first one,
a={1: 'a', 2: 'b', 3: 'c', 4: 'd'} b=[x x in if x in (1,2,3)] b=['a', 'b', 'c']
second one,
a={1: 'a', 2: 'b', 3: 'c', 4: 'd'} c=[a[x] x in set(a.keys()) & set([1,2,3])] b=['a', 'b', 'c']
i know 1 works better in terms of memory optimized methods, , large sets of data.
thanks in advance!
if you're optimizing memory use generators tool. example:
def get_keys(mapping, keys): key in keys: try: yield mapping[key] except keyerror: continue
on example:
list(get_keys(a, (1, 2, 3))) ['a', 'b', 'c']
Comments
Post a Comment