What is the best way to implement nested dictionaries?

后端 未结 21 1808
[愿得一人]
[愿得一人] 2020-11-22 00:29

I have a data structure which essentially amounts to a nested dictionary. Let\'s say it looks like this:

{\'new jersey\': {\'mercer county\': {\'plumbers\':          


        
21条回答
  •  无人共我
    2020-11-22 00:52

    I like the idea of wrapping this in a class and implementing __getitem__ and __setitem__ such that they implemented a simple query language:

    >>> d['new jersey/mercer county/plumbers'] = 3
    >>> d['new jersey/mercer county/programmers'] = 81
    >>> d['new jersey/mercer county/programmers']
    81
    >>> d['new jersey/mercer country']
    
    

    If you wanted to get fancy you could also implement something like:

    >>> d['*/*/programmers']
    
    

    but mostly I think such a thing would be really fun to implement :D

提交回复
热议问题