nested dictionary python

前端 未结 3 2012
孤城傲影
孤城傲影 2021-01-03 15:14

How do I create a nested dictionary in python So, I want the data be in this form..

{Category_id: {Product_id:... productInstance},{prod_id_1: this instance}         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-03 15:46

    Check my NestedDict class here: https://stackoverflow.com/a/16296144/2334951

    >>> branches = [b for b in data.paths()]
    >>> ['plumbers' in k for k in branches]
    [True, False, True, False, False, False]
    >>> any(['plumbers' in k for k in branches])
    True
    >>> [k for k in branches if 'plumbers' in k]
    [['new york', 'queens county', 'plumbers'], ['new jersey', 'mercer county', 'plumbers']]
    >>> [data[k] for k in branches if 'plumbers' in k]
    [9, 3]
    

    I hope that with some intuition this example covers the question.

提交回复
热议问题