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]
添加回答
举报