我有一本下面的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
像这样的怎么样?你知道吗
然后可以使用Counter object从那里计算元素。你知道吗
您可以使用
collections.Counter
:帮助(计数器):
相关问题 更多 >
编程相关推荐