querytimes = "SELECT video_id, COUNT(src_ip) FROM video GROUP BY video_id ORDER BY COUNT(src_ip) DESC"
cur.execute (querytimes)
dltimes = cur.fetchall()
for row in dltimes:
videoid = str(row [0])
downloadtimes = int(str(row [1]))
x.append(rank)
rank = rank + 1
y.append(downloadtimes)
v.append(videoid)
counter = dict(zip(v, y))
for n in xrange(1, max(counter.itervalues()) + 1):
perc = 100. / sum(1 for nb in counter.itervalues() if nb == n) / len(counter)
if perc:
print '%.f%% videos have been downloaded %d times' % (perc, n)
This is my code. At first I read data from a database, then put videoid and downloadtimes into a dictionary, and do some calculation, but I got the following error:
perc = 100. / sum(1 for nb in counter.itervalues() if nb == n) / len(counter)
ZeroDivisionError: float division
Can anyone help a bit?
forloop right? theifpart ought to be inside the loop, otherwise it'll only execute once