简体   繁体   中英

Update nested dict with missing key value pairs

I have a dict with a unique id and nested dicts of key-value pairs corresponding to cardinal positions:

{'925328413719459770': {'N': 14,
  'NNW': 116,
  'NW': 38,
  'S': 1,
  'SE': 2,
  'SSE': 1,
  'W': 16,
  'WNW': 146,
  'WSW': 16}}

However, not all nested dicts will contain each of the 16 possible cardinal positions:

cp = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
        "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"]

I'm able to get the difference between the set of cardinal positions in the dict compared to the full list cp

for leg in legdict.values():
    print(list(set(cp) - set(leg.keys())))
    
['ESE', 'SSW', 'NNE', 'NE', 'ENE', 'SW', 'E']

How would one then go about updating the nested dict with the missing positions and setting their values to 0? The output would be something like this:

  {'925328413719459770': {'N': 14,
      'NNW': 116,
      'NW': 38,
      'S': 1,
      'SE': 2,
      'SSE': 1,
      'W': 16,
      'WNW': 146,
      'WSW': 16,
      'ESE': 0, 
      'SSW': 0, 
      'NNE': 0, 
      'NE': 0, 
      'ENE':0, 
      'SW': 0, 
      'E': 0 }}

Is there an efficient way of doing this in python using the update() method, or can a list be used to update a dict while setting all elements of that list to 0? The actual legdict is very large so any suggestions about an efficient method would be appreciated.

What about this:

cardinal_positions = [
    "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
    "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"
]

def fill_missing_keys(the_huge_dict):
    """Fills in missing cardinal positions with 0"""

    for inner_dict in the_huge_dict.values():
        for key in cardinal_positions:
            if key not in inner_dict:
                inner_dict[key] = 0

Should be reasonably efficient in that it doesn't make any copies, it just iterates over everything and fills in missing keys where necessary.

As a general rule, you should always just go with the simplest solution first. If it turns out to be too slow, optimize later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM