计算元组中的元素

时间:2015-11-02 10:50:57

标签: python loops matplotlib tuples

我有很多票据字典都是这样的:

{'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制作一个饼图,以显示有多少票是'新','开发'或'关闭',因此我需要检查"status-events"我的最后一个值我试过这样但它不起作用,我的计数器不会增加。

newCounter = 0
closedCounter = 0
developingCounter = 0


for (i,j) in OpenedDict.iteritems():
    if j['status-events'][-1]== 'new':
        newCounter += 1
    if j['status-events'][-1]== 'closed':
        closedCounter += 1
    if j['status-events'][-1]== 'developing':
        developingCounter += 1

3 个答案:

答案 0 :(得分:2)

以下应该做你需要的。它利用Python的Counter类来计算每个status-events类型,然后在饼图中显示整理的信息:

import matplotlib.pyplot as plt
import datetime
from collections import Counter

events = [
    {'origin': u'HW', 
    'department': u'Intern', 
    'ticket-closed': False, 
    'prio-events': [(datetime.datetime(2015, 6, 8, 17, 30, 17, 490990), u'Important')], 
    'status-events': [(datetime.datetime(2015, 6, 8, 17, 30, 17, 490990), u'new'), (datetime.datetime(2015, 7, 22, 16, 41, 52, 408334), u'developing')]},

    {'origin': u'HW', 
    'department': u'Intern', 
    'ticket-closed': False, 
    'prio-events': [(datetime.datetime(2015, 6, 8, 17, 39, 34, 351329), u'Important')], 
    'status-events': [(datetime.datetime(2015, 6, 8, 17, 39, 34, 351329), u'new')]},

    {'origin': u'HW', 
    'department': u'Intern', 
    'ticket-closed': False, 
    'prio-events': [(datetime.datetime(2015, 6, 8, 17, 49, 58, 454331), u'Important')], 
    'status-events': [(datetime.datetime(2015, 6, 8, 17, 49, 58, 454331), u'new'), (datetime.datetime(2015, 7, 22, 16, 42, 49, 339349), u'closed')]}]

event_types = Counter()

for event in events:
    for status in event['status-events']:
        event_types[status[1]] += 1

plt.pie(event_types.values(), labels=event_types.keys(), autopct='%1.1f%%')
plt.axis('equal')
plt.show()

这将显示如下的饼图:

enter image description here

答案 1 :(得分:1)

看起来您正在读取错误的元素来计算,status-events是一个元组列表。

要查找无法正确阅读的原因,请尝试将j['status-events'][-1]打印到终端,以查看您要比较的值。现在,这将返回一个元组。

如果您只关心status-events列表中的最后一项,则可以将脚本修改为:

for (i,j) in OpenedDict.iteritems():
    if j['status-events'][-1][-1] == 'new':
        newCounter += 1
    if j['status-events'][-1][-1] == 'closed':
        closedCounter += 1
    if j['status-events'][-1][-1] == 'developing':
        developingCounter += 1

否则,如果你想要列表status-events中每个项目的值,你必须为它创建另一个循环:

for (i,j) in OpenedDict.iteritems():
    for k in j['status-events']:
        if k[-1] == 'new':
            newCounter += 1
        if k[-1] == 'closed':
            closedCounter += 1
        if k[-1] == 'developing':
            developingCounter += 1

除此之外,在第二个例子中似乎缺少一个括号。

答案 2 :(得分:1)

如果OpenedDict确实是列出的dict之一,那就是MVCE看起来像是:

OpenedDict = {'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')]}

newCounter = 0
closedCounter = 0
developingCounter = 0

for (i,j) in OpenedDict.iteritems():
    if j['status-events'][-1]== 'new':
        newCounter += 1
    if j['status-events'][-1]== 'closed':
        closedCounter += 1
    if j['status-events'][-1]== 'developing':
        developingCounter += 1

然后您遇到了问题,因为您正在重复项目,然后调查这些值。 ij例如会使用以下值:

i = 'origin'
j = u'HW'

i = 'department'
j = u'Intern'

i = 'ticked-closed'
j = False

等。您无法期望找到任何“状态 - 事件”#39;在那些价值观中。相反,你必须直接检查这样的字典。类似的东西:

OpenedDict = {'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')]}

newCounter = 0
closedCounter = 0
developingCounter = 0

if OpenedDict['status-events'][-1]== 'new':
    newCounter += 1
if OpenedDict['status-events'][-1]== 'closed':
    closedCounter += 1
if OpenedDict['status-events'][-1]== 'developing':
    developingCounter += 1