How To Use Dict.get() With Multidimensional Dict?
I have a multidimensional dict, and I'd like to be able to retrieve a value by a key:key pair, and return 'NA' if the first key doesn't exist. All of the sub-dicts have the same ke
Solution 1:
How about
d.get('a', {'j': 'NA'})['j']
?
If not all subdicts have a j
key, then
d.get('a', {}).get('j', 'NA')
To cut down on identical objects created, you can devise something like
classDefaultNASubdict(dict):
classNADict(object):
def__getitem__(self, k):
return'NA'
NA = NADict()
def__missing__(self, k):
return self.NA
nadict = DefaultNASubdict({
'a': {'j':1,'k':2},
'b': {'j':2,'k':3},
'd': {'j':1,'k':3}
})
print nadict['a']['j'] # 1print nadict['b']['j'] # 2print nadict['c']['j'] # NA
Same idea using defaultdict
:
import collections
classNADict(object):def__getitem__(self, k):
return'NA'@staticmethoddefinstance():
return NADict._instance
NADict._instance = NADict()
nadict = collections.defaultdict(NADict.instance, {
'a': {'j':1,'k':2},
'b': {'j':2,'k':3},
'd': {'j':1,'k':3}
})
Solution 2:
Another way to get multidimensional dict example ( use get method twice)
d.get('a', {}).get('j')
Solution 3:
Here's a simple and efficient way to do it with ordinary dictionaries, nested an arbitrary number of levels. The example code works in both Python 2 and 3.
from __future__ import print_function
try:
from functools import reduce
except ImportError: # Assume it's built-in (Python 2.x)passdefchained_get(dct, *keys):
SENTRY = object()
defgetter(level, key):
return'NA'if level is SENTRY else level.get(key, SENTRY)
return reduce(getter, keys, dct)
d = {'a': {'j': 1, 'k': 2},
'b': {'j': 2, 'k': 3},
'd': {'j': 1, 'k': 3},
}
print(chained_get(d, 'a', 'j')) # 1print(chained_get(d, 'b', 'k')) # 3print(chained_get(d, 'k', 'j')) # NA
It could also be done recursively:
# Recursive version.defchained_get(dct, *keys):
SENTRY = object()
defgetter(level, keys):
return (level if keys[0] is SENTRY else'NA'if level is SENTRY else
getter(level.get(keys[0], SENTRY), keys[1:]))
return getter(dct, keys+(SENTRY,))
Although this way of doing it isn't quite as efficient as the first.
Solution 4:
Rather than a hierarchy of nested dict
objects, you could use one dictionary whose keys are a tuple representing a path through the hierarchy.
In [34]: d2 = {(x,y):d[x][y] for x in d for y in d[x]}
In [35]: d2
Out[35]:
{('a', 'j'): 1,
('a', 'k'): 2,
('b', 'j'): 2,
('b', 'k'): 3,
('d', 'j'): 1,
('d', 'k'): 3}
In [36]: timeit [d[x][y] for x,y in d2.keys()]
100000 loops, best of 3: 2.37 us per loop
In [37]: timeit [d2[x] for x in d2.keys()]
100000 loops, best of 3: 2.03 us per loop
Accessing this way looks like it's about 15% faster. You can still use the get
method with a default value:
In [38]: d2.get(('c','j'),'NA')
Out[38]: 'NA'
Post a Comment for "How To Use Dict.get() With Multidimensional Dict?"