1 回答

TA貢獻1773條經驗 獲得超3個贊
這些值{'Ready Date', 'Ready Time', 'Delivery Date', 'Service Level'}組成一個集合,它們不是內部字典的鍵,但仍然可以檢查它們是否存在于原始字典中x:
已實現的dictionary_to_list函數采用原始字典x并將其展平為一個列表,該列表包含列表中的所有鍵和值。
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]
添加回答
舉報