jeudi 27 octobre 2016

Python - Cant get out of from a for if loop

I'm having some trouble getting out from a for if loop that goes like this:

data = self.coll.find(myMongodbQueryfields...)
days =  []
days_count = {}
date_from = "2016-07-10"
date_to = "2016-08-10"

try:
    for x in data:
        time_formated = dt.datetime.strptime(str(x["user_id"]["dt"]),
                                             '%a %b %d %H:%M:%S +0000 %Y').strftime("%Y-%m-%d")
        if time_formated > date_from and time_formated < date_to:
            days.append(time_formated)

except KeyError:
    pass


for x in days:
    if x in days_count:
        days_count[x] += 1
    else:
        days_count[x] = 1

return mygraph.plot(the graph uses days_count values for x and days_count values for y )

The problem is that it returns the same graph two times when all I want is one graph. I guess the problem is because the function goes through the for loop two times but what am I doing wrong? I know it's simple bit can't see it.

Aucun commentaire:

Enregistrer un commentaire