我有很多票务字典都是这样的:
{'origin': u'HW',
'department': u'Intern',
'ticket-closed': False,
'prio-events': [(datetime.datetime(2015, 6, 8, 17, 30, 17, 490990, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'Important')],
'status-events': [(datetime.datetime(2015, 6, 8, 17, 30, 17, 490990, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'new'), (datetime.datetime(2015, 7, 22, 16, 41, 52, 408334, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'developing')]}
{'origin': u'HW',
'department': u'Intern',
'ticket-closed': False,
'prio-events': [(datetime.datetime(2015, 6, 8, 17, 39, 34, 351329, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'Important')],
'status-events': [(datetime.datetime(2015, 6, 8, 17, 39, 34, 351329, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'new')}
{'origin': u'HW',
'department': u'Intern',
'ticket-closed': False,
'prio-events': [(datetime.datetime(2015, 6, 8, 17, 49, 58, 454331, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'Important')],
'status-events': [(datetime.datetime(2015, 6, 8, 17, 49, 58, 454331, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'new'), (datetime.datetime(2015, 7, 22, 16, 42, 49, 339349, tzinfo=<LocalTimezone "UTC+02:00" 2:00:00>), u'closed')]}
我想在matplotlib
中制作一个饼图来显示有多少票是“new”、“developing”或“closed”,因此我需要检查"status-events"
的最后一个值。我试过这样做,但它不起作用,我的计数器不会增加。在
看起来你读错了要计数的元素,状态事件是元组的列表。在
要跟踪它没有被正确读取的原因,请尝试将
j['status-events'][-1]
打印到终端,以查看要比较的值。现在,这将返回一个元组。在如果只关心
status-events
列表中的最后一项,则可以将脚本修改为:否则,如果需要列表
^{pr2}$status-events
中每个项目的值,则必须为其创建另一个循环:除此之外,在第二个例子中似乎缺少一个括号。在
下面的内容可以满足您的需要。它使用Python的
Counter
类来计算status-events
类型中的每一个,然后在饼图中显示整理后的信息:这将显示如下饼图:
如果
OpenedDict
确实是所列的dict
之一,也就是说MVCE看起来像:然后你就遇到了一个问题,因为你要迭代这些项,然后调查这些值。}将取如下值:
^{pr2}$i
和{你不能期望在这些值中找到任何“状态事件”。取而代之的是你必须直接查阅这样一本词典。比如:
相关问题 更多 >
编程相关推荐