创建与数据库中的数据相同的列表列表给出空列表

时间:2015-05-11 15:25:42

标签: django list python-2.7

我试图绘制一张图表,显示同一地块中每天每分钟平均呼叫持续时间为7天,现在我定义了根据条件向我提供数据的功能这将被绘制,但我总是得到一个空列表的列表。任何人都可以帮我解决这个问题? (acc只是全球数据库中数据的一个例子) 这是功能:

import time
import calendar
from datetime import datetime
from itertools import repeat
acc=[{u'switch_id': 3, u'hangup_cause_id': 7, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 1), u'duration': 32}, {u'switch_id': 3, u'hangup_cause_id': 10, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 8), u'duration': 20}, {u'switch_id': 3, u'hangup_cause_id': 10, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 10), u'duration': 17}]
t = datetime.now()
y = t.year
m = t.month
d = t.day
donnees=[]
for k in range(7):
    try:
        m = t.month
        data=[]
        liste=[]
        liste_time=[]
        for i in acc:
            if (i["start_uepoch"].year == y and i["start_uepoch"].month == m and i["start_uepoch"].day == d-k):
            liste.append([i["start_uepoch"],i["duration"]])
        for q in range(24):
            for mnt in range(60):
                liste2=[]
                ACD=0
                somme_duration=0
                n=0
                for p in liste:
                    if (p[0].hour==q and p[0].minute == mnt):
                        liste2.append(p[1])
                        temps=p[0]
                if len(liste2)!=0:
                    for j in liste2:
                        somme_duration+=j
                        n+=1
                    ACD=round((float(somme_duration)/n)*100)/100
                    liste_time.append(calendar.timegm(temps.timetuple()))
                    data.append(ACD)                               
                else:
                    liste_time.append(calendar.timegm(temps.timetuple()))
                    data.append(0)
    except:
        pass
    donnees.append(data)
print donnees

1 个答案:

答案 0 :(得分:0)

这是由于您的尝试/排除条件,如果您在循环后通过设置temps = None将其删除,它会解决您的问题:

import time
import calendar
from datetime import datetime
from itertools import repeat
acc=[{u'switch_id': 3, u'hangup_cause_id': 7, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 1), u'duration': 32}, {u'switch_id': 3, u'hangup_cause_id': 10, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 8), u'duration': 20}, {u'switch_id': 3, u'hangup_cause_id': 10, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 10), u'duration': 17}]
t = datetime.now()
y = t.year
m = t.month
d = t.day
donnees=[]
for k in range(7):
    m = t.month
    data=[]
    liste=[]
    liste_time=[]
    for i in acc:
        if (i["start_uepoch"].year == y and i["start_uepoch"].month == m and i["start_uepoch"].day == d-k):
            liste.append([i["start_uepoch"],i["duration"]])
    for q in range(24):
        for mnt in range(60):
            temps = None
            liste2=[]
            ACD=0
            somme_duration=0
            n=0
            for p in liste:
                if (p[0].hour==q and p[0].minute == mnt):
                    liste2.append(p[1])
                    temps=p[0]
            if temps:
                if len(liste2)!=0:
                    for j in liste2:
                        somme_duration+=j
                        n+=1
                    ACD=round((float(somme_duration)/n)*100)/100
                    liste_time.append(calendar.timegm(temps.timetuple()))
                    data.append(ACD)                               
                else:
                    liste_time.append(calendar.timegm(temps.timetuple()))
                    data.append(0)
    donnees.append(data)
print donnees