What is the best way to implement nested dictionaries?

后端 未结 21 1815
[愿得一人]
[愿得一人] 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:42

    For the following (copied from above) is there a way to implement the append function. I am trying to use a nested dictionary to store values as array.

    class Vividict(dict):
        def __missing__(self, key):
            value = self[key] = type(self)() # retain local pointer to value
        return value  
    

    My current implementation is as follows:

    totalGeneHash=Vividict()
            
    for keys in GenHash:
        for second in GenHash[keys]:
            if keys in sampleHash:
                total_val = GenHash[keys][second]
                    totalGeneHash[gene][keys].append(total_val)
    This is the error I get: AttributeError: 'Vividict' object has no attribute 'append'
    

提交回复
热议问题