1 回答
TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊
這些值{'Ready Date', 'Ready Time', 'Delivery Date', 'Service Level'}組成一個(gè)集合,它們不是內(nèi)部字典的鍵,但仍然可以檢查它們是否存在于原始字典中x:
已實(shí)現(xiàn)的dictionary_to_list函數(shù)采用原始字典x并將其展平為一個(gè)列表,該列表包含列表中的所有鍵和值。
x = {'test': {'shipmentInfo': {'Ready Date', 'Ready Time', 'Delivery Date', 'Service Level'}}}
check_list = ["test", "shipmentInfo", "Ready Date","Ready Time","Delivery Date","Service Level"]
def dictionary_to_list_helper(d, l):
for k, v in d.items():
l.append(k)
if isinstance(v, list) or isinstance(v, set):
for item in v:
l.append(item)
elif isinstance(v, dict):
dictionary_to_list_helper(v, l)
def dictionary_to_list(d):
l = []
dictionary_to_list_helper(d, l)
return l
missing = [field for field in dictionary_to_list(x) if field not in check_list]
if len(missing) == 0:
print("All values are entered")
else:
[print(f"Missing value: {field}") for field in missing]
添加回答
舉報(bào)
