A
A
Artyom Innokentiev2015-09-23 10:57:23
Python
Artyom Innokentiev, 2015-09-23 10:57:23

How to get data from such a dictionary?

There is a dictionary:
data ={'a': {'b': {'c': 1}}}
You need to get the value of the key 'c' using this construction data[key], while this option does not fit data['a']['b']['c']. How to do it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
angru, 2015-09-23
@artinnok

Python 3. but this is a crutch of course.

from functools import reduce


class CustomDict(dict):
    def __getitem__(self, key):
        return reduce(lambda acc, x: acc.__getitem__(x), key.split('.'), super())


data = CustomDict({'a': {'b': {'c': 1}}})

print(data['a.b.c'])

A
Anton Fedoryan, 2015-09-23
@AnnTHony

The meaning is the same as

data['a']['b']['c']

((data.get('a')).get('b')).get('c')

L
lyeskin, 2015-09-23
@lyeskin

https://github.com/akesterson/dpath-python

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question