我有以下 python 字典
resultDict:
{'1234':{'alertStatus': 'open', 'reasonDescription': None},
'4321': {'alertStatus': 'closed', 'reasonDescription': 'Public'},
'6789': {'alertStatus': 'open', 'reasonDescription': 'None'}}
我想计算打开和关闭警报的数量(实际上我有 5 种不同的状态,但对于这个示例,我已将其减少到 2)
我已经编写了以下代码,但它看起来很不整洁。我想知道是否有更好的方法来做到这一点
result = {}
result['length'] = len(resultDict)
lenOpen = 0
lenClosed = 0
for notifications in resultDict.values():
if notifications['alertStatus'] == 'open':
lenOpen = lenOpen + 1
if notifications['alertStatus'] == 'closed':
lenClosed = lenClosed + 1
statusCount = []
if lenOpen > 0:
statusCount.append(str(lenOpen) + ' ' + 'open')
if lenOpenUnderInvestigation > 0:
statusCount.append(str(lenClosed) + ' ' +'closed')
result['statusCount'] = statusCount