填充本身在列表中的字典的值

Filling up values of a dict, which in itself is in a list

我有问题。我有一个 list,它又包含 dicts。问题是 dicts 可以有不同的大小。某些 dicts 中缺少某些元素。是否可以选择用缺少的属性填充 dicts?这只是填充 keys 的问题,并且可以简单地用 None.

填充值

下面的例子

[
{'_id': 'orders/213123',
 'contactEditor': {'name': 'Max Power',
                   'phone': '1234567'},
 'isCompleteDelivery': False,
},
{'_id': 'orders/2173823',
 'contactEditor': {'name': 'Michael'},
},
]

原来第一个元素最多keys第二个元素少了下面keysphoneisCompleteDelivery.

这是填写的,看起来像这样

[
{'_id': 'orders/213123',
 'contactEditor': {'name': 'Max Power',
                   'phone': '1234567'},
 'isCompleteDelivery': False,
},
{'_id': 'orders/2173823',
 'contactEditor': {'name': 'Michael',
                   'phone': None},
 'isCompleteDelivery': None,
},
]

这给了我哪个dict有最多的元素和数量。

%%time
highest_element = 0
elements = 0
for i, element in enumerate(myList):
    counted = count(myList[i])
    if(counted > elements):
        elements = counted
        highest_element = i
        
print(str(elements) + " " + str(highest_element))
myList = [
{'_id': 'orders/213123',
 'contactEditor': {'name': 'Max Power',
  'phone': '1234567',
  'email': 'max@power.com'},
 'contactSoldToParty': {'name': 'Max Not',
  'phone': '123456789',
  'email': 'maxnot@power.com'},
 'isCompleteDelivery': False,
 'metaData': {'dataOriginSystem': 'Goods',
  'dataOriginWasCreatedTime': '10:12:12',},
 'orderDate': '2021-02-22',
 'orderDateBuyer': '2021-02-22',
},
{'_id': 'orders/12323',
 'contactEditor': {'name': 'Max Power2',
  'phone': '1234567',
  'email': 'max@power.com'},
 'contactSoldToParty': {'name': 'Max Not',
  'phone': '123456789',
  'email': 'maxnot@power.com'},
 'isCompleteDelivery': False,
 'metaData': {'dataOriginSystem': 'Goods',
  'dataOriginWasCreatedTime': '10:12:12',},
 },
{'_id': 'orders/12323',
 'contactEditor': {'name': 'Max Power2',
  'phone': '1234567',
  'email': 'max@power.com'},
 'contactSoldToParty': {'name': 'Max Not',
  'phone': '123456789',
  'email': 'maxnot@power.com'},
 'orderDate': '2021-02-22',
 'orderDateBuyer': '2021-02-22',
 },
{'_id': 'orders/12323',
 'contactEditor': {'name': 'Max Power2',
  'phone': '1234567',
  'email': 'max@power.com'},
 'contactSoldToParty': {'name': 'Max Not',},
 'isCompleteDelivery': False,
 },
]

如果字典中不存在值 None,此代码将创建一个键。

lst = [
{'_id': 'orders/213123',
 'contactEditor': {'name': 'Max Power',
                   'phone': '1234567'},
 'isCompleteDelivery': False,
},
{'_id': 'orders/2173823',
 'contactEditor': {'name': 'Michael'},
},
]

complete_dict = {'_id': None,
 'contactEditor': {'name': None,
                   'phone': None},
 'isCompleteDelivery': None,
} 

# here complete dictionary means which is contain all possible keys.
all_keys = complete_dict.keys() 
for i,value in enumerate(lst):
    for a in all_keys:
        try:
            value[a]
        except KeyError:
            value[a]=None
        else:
            v = value[a]
            if isinstance(v,dict):
                for a2 in complete_dict[a].keys():
                    try:
                        v[a2]
                    except KeyError:
                        v[a2]=None
                value[a]=v

    del lst[i]
    lst.insert(i,value)
print(lst)

输出

[{'_id': 'orders/213123',
 'contactEditor': {'name': 'Max Power', 'phone': '1234567'},
 'isCompleteDelivery': False},
 {'_id': 'orders/2173823',
 'contactEditor': {'name': 'Michael', 'phone': None}, 
 'isCompleteDelivery': None}]

如果我理解正确的话,看来你需要构造一个所有可能键的超集。一旦你有了它,你就可以遍历列表并在每个字典中设置默认值。既然已经调整了“顶级”词典,您需要降低一个级别并对嵌套词典执行相同的操作。此代码仅限于问题中所示的数据结构。

list_ = [
    {'_id': 'orders/213123',
     'contactEditor': {'name': 'Max Power',
                       'phone': '1234567'},
     'isCompleteDelivery': False,
     },
    {'_id': 'orders/2173823',
        'contactEditor': {'name': 'Michael'},
     }
]

def modkv(L, K):
    superset_ = set(key for dict_ in list_ for key in dict_[K])
    for dict_ in L:
        for key in superset_:
            dict_[K].setdefault(key, None)

superset = set(key for dict_ in list_ for key in dict_)

for dict_ in list_:
    for key in superset:
        dict_.setdefault(key, None)

for dict_ in list_:
    for k, v in dict_.items():
        if isinstance(v, dict):
            modkv(list_, k)

print(list_)

输出:

[{'_id': 'orders/213123', 'contactEditor': {'name': 'Max Power', 'phone': '1234567'}, 'isCompleteDelivery': False}, {'_id': 'orders/2173823', 'contactEditor': {'name': 'Michael', 'phone': None}, 'isCompleteDelivery': None}]