总结一个字典中有多少字段为空或不存在
Sum up how many fields are empty or do not exist in a dict
我有问题。我在这些列表中有一个列表 myList
,还有一个字典。我想统计字段 dataOriginSystem
是否为空或不存在。不幸的是我得到了错误的结果。 if(key_nested == 'dataOriginSystem'): ... else: count =+ 1
原因在于 if 查询。既然我在问,这个领域是否存在?如果不是,则将其加起来,因为我遍历了所有嵌套键,所以这里有一个错误。另外,有什么办法可以提高效率吗?
如何查询dataOriginSystem
字段中有多少是空的或不存在的?
count = 0
for element in myList:
for key in element.keys():
if(key == 'metaData'):
for key_nested in element[key].keys():
if(key_nested == 'dataOriginSystem'):
if(key_nested == None):
count += 1
else:
count += 1
print(count)
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',},
'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': {
'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': None,
'dataOriginWasCreatedTime': '10:12:12',},
'orderDate': '2021-02-22',
'orderDateBuyer': '2021-02-22',
},
]
结果应该是
[OUT] 2
# Because of the two last elements.
# The first element does not exist
# and the second ist None.
您不必循环键。直接访问您想要的项目并在未找到该项目或找到但 None.
时递增计数器
count = 0
for element in myList:
if element["metaData"].get("dataOriginSystem", None) is None:
count += 1
print(count)
可以直接在嵌套键上使用dict.get
,返回默认值None
,然后统计得到的None
个值:
sum(d['metaData'].get('dataOriginSystem', None) is None for d in myList)
输出
2
你可以试试这样的...
non_existant = len([0 for item in myList if item['metaData'].get('dataOriginSystem') == None or item['metaData'].get('dataOriginSystem') == ''])
print(non_existant)
输出...
2
我有问题。我在这些列表中有一个列表 myList
,还有一个字典。我想统计字段 dataOriginSystem
是否为空或不存在。不幸的是我得到了错误的结果。 if(key_nested == 'dataOriginSystem'): ... else: count =+ 1
原因在于 if 查询。既然我在问,这个领域是否存在?如果不是,则将其加起来,因为我遍历了所有嵌套键,所以这里有一个错误。另外,有什么办法可以提高效率吗?
如何查询dataOriginSystem
字段中有多少是空的或不存在的?
count = 0
for element in myList:
for key in element.keys():
if(key == 'metaData'):
for key_nested in element[key].keys():
if(key_nested == 'dataOriginSystem'):
if(key_nested == None):
count += 1
else:
count += 1
print(count)
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',},
'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': {
'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': None,
'dataOriginWasCreatedTime': '10:12:12',},
'orderDate': '2021-02-22',
'orderDateBuyer': '2021-02-22',
},
]
结果应该是
[OUT] 2
# Because of the two last elements.
# The first element does not exist
# and the second ist None.
您不必循环键。直接访问您想要的项目并在未找到该项目或找到但 None.
时递增计数器count = 0
for element in myList:
if element["metaData"].get("dataOriginSystem", None) is None:
count += 1
print(count)
可以直接在嵌套键上使用dict.get
,返回默认值None
,然后统计得到的None
个值:
sum(d['metaData'].get('dataOriginSystem', None) is None for d in myList)
输出
2
你可以试试这样的...
non_existant = len([0 for item in myList if item['metaData'].get('dataOriginSystem') == None or item['metaData'].get('dataOriginSystem') == ''])
print(non_existant)
输出...
2