python—统计特定时间发布数量(列表、range、for、元组、字典)
题意:对列表中(‘2012-11-3,22’, 12)……..24个时间区段,即每个小时发布的数量统计,例子中22为22点,12为发布次数。有可能是不同日期的22点发布次数,就要对所有日期的22点进行统计次数。
方法1:使用for n in range(0,len(test))语句
Python源码:
root@kali:~/python/laowangpy/datadig# cat sometime.py
# -*-coding:utf-8 -*-
test = [('2012-11-3,22', 12), ('2012-11-4,5', 8), ('2012-11-4,0', 7), ('2012-11-4,2', 7), ('2012-11-4,6', 7), ('2012-11-2,16', 5), ('2012-11-3,12', 5), ('2012-11-3,13', 5), ('2012-11-4,4', 5), ('2012-11-2,13', 4), ('2012-11-2,7', 4), ('2012-11-3,11', 4), ('2012-11-3,15', 4), ('2012-11-3,19', 4), ('2012-11-4,10', 4), ('2013-11-3,22', 4), ('2012-10-30,6', 3), ('2012-11-2,0', 3), ('2012-11-3,16', 3), ('2012-11-3,20', 3), ('2012-11-3,23', 3), ('2012-11-4,8', 3), ('2012-11-2,12', 2), ('2012-11-3,14', 2), ('2012-11-3,2', 2), ('2012-11-3,21', 2), ('2012-11-3,3', 2), ('2012-11-4,3', 2), ('2012-11-4,7', 2), ('2012-11-4,9', 2), ('2013-11-3,19', 2), ('2013-11-4,5', 2), ('2012-10-30,9', 1), ('2012-11-1,17', 1), ('2012-11-1,5', 1), ('2012-11-2,11', 1), ('2012-11-3,0', 1), ('2012-11-3,1', 1), ('2012-11-3,18', 1), ('2012-11-3,5', 1), ('2012-11-4,1', 1), ('2013-10-29,10', 1), ('2013-10-4,5', 1), ('2013-11-2,16', 1), ('2013-11-3,10', 1), ('2013-11-3,13', 1), ('2013-11-3,20', 1), ('2013-11-3,21', 1), ('2013-11-4,0', 1), ('2013-11-4,1', 1), ('2013-11-4,10', 1), ('2013-11-4,3', 1), ('2013-11-4,4', 1), ('2013-11-4,7', 1), ('2013-12-4,0', 1), ('2014-11-3,11', 1), ('2014-11-3,15', 1), ('2014-11-3,22', 1), ('2014-11-4,4', 1), ('2014-12-1,2', 1), ('2014-12-3,13', 1)]
i = 0
tupe = [0]*24#新建列表中24个元素
for i in range(0,len(test)):#i遍历列表test的长度,从0开始
tupe[int(test[i][0].split(',')[1])] = tupe[int(test[i][0].split(',')[1])] + test[i][1]
#test[i][1]取列表[i]中的第二个元素的值
#tupe[int(test[i][0].split(',')[1])]对列表[i]中第一个元素,使用,分割,并且把分割成的第二个元素取出来,加入列表tupe中
#tupe[int(test[i][0].split(',')[1])]为tupe[x]的值
#int(test[i][0].split(',')[1])为tupe[x]的x值
print tupe
time = 0#次数初始化
for n in range(0,len(tupe)):#n遍历列表tupe长度,从0开始
print "the clock's %d have %s times" %(time,tupe[n])#24个小时内每个小时发布次数
time = time + 1#每次自增1
root@kali:~/python/laowangpy/datadig#
python运行情况:
root@kali:~/python/laowangpy/datadig# python sometime.py
[13, 3, 10, 5, 7, 13, 10, 7, 3, 3, 7, 6, 7, 11, 2, 5, 9, 1, 1, 6, 4, 3, 17, 3]
the clock's 0 have 13 times
the clock's 1 have 3 times
the clock's 2 have 10 times
the clock's 3 have 5 times
the clock's 4 have 7 times
the clock's 5 have 13 times
the clock's 6 have 10 times
the clock's 7 have 7 times
the clock's 8 have 3 times
the clock's 9 have 3 times
the clock's 10 have 7 times
the clock's 11 have 6 times
the clock's 12 have 7 times
the clock's 13 have 11 times
the clock's 14 have 2 times
the clock's 15 have 5 times
the clock's 16 have 9 times
the clock's 17 have 1 times
the clock's 18 have 1 times
the clock's 19 have 6 times
the clock's 20 have 4 times
the clock's 21 have 3 times
the clock's 22 have 17 times
the clock's 23 have 3 times
root@kali:~/python/laowangpy/datadig# vi sometime.py
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
方法2:使用字典
python源码:
root@kali:~/python/laowangpy/datadig# cat sometime.py
# -*-coding:utf-8 -*-
test = [('2012-11-3,22', 12), ('2012-11-4,5', 8), ('2012-11-4,0', 7), ('2012-11-4,2', 7), ('2012-11-4,6', 7), ('2012-11-2,16', 5), ('2012-11-3,12', 5), ('2012-11-3,13', 5), ('2012-11-4,4', 5), ('2012-11-2,13', 4), ('2012-11-2,7', 4), ('2012-11-3,11', 4), ('2012-11-3,15', 4), ('2012-11-3,19', 4), ('2012-11-4,10', 4), ('2013-11-3,22', 4), ('2012-10-30,6', 3), ('2012-11-2,0', 3), ('2012-11-3,16', 3), ('2012-11-3,20', 3), ('2012-11-3,23', 3), ('2012-11-4,8', 3), ('2012-11-2,12', 2), ('2012-11-3,14', 2), ('2012-11-3,2', 2), ('2012-11-3,21', 2), ('2012-11-3,3', 2), ('2012-11-4,3', 2), ('2012-11-4,7', 2), ('2012-11-4,9', 2), ('2013-11-3,19', 2), ('2013-11-4,5', 2), ('2012-10-30,9', 1), ('2012-11-1,17', 1), ('2012-11-1,5', 1), ('2012-11-2,11', 1), ('2012-11-3,0', 1), ('2012-11-3,1', 1), ('2012-11-3,18', 1), ('2012-11-3,5', 1), ('2012-11-4,1', 1), ('2013-10-29,10', 1), ('2013-10-4,5', 1), ('2013-11-2,16', 1), ('2013-11-3,10', 1), ('2013-11-3,13', 1), ('2013-11-3,20', 1), ('2013-11-3,21', 1), ('2013-11-4,0', 1), ('2013-11-4,1', 1), ('2013-11-4,10', 1), ('2013-11-4,3', 1), ('2013-11-4,4', 1), ('2013-11-4,7', 1), ('2013-12-4,0', 1), ('2014-11-3,11', 1), ('2014-11-3,15', 1), ('2014-11-3,22', 1), ('2014-11-4,4', 1), ('2014-12-1,2', 1), ('2014-12-3,13', 1)]
n = 0
timeclock = [0]*24#定义24个时间段
#print timeclock
counttimes = {}#初始24个时间对应次数的字典
for n in range(0,len(test)):
timeclock[int(test[n][0].split(',')[1])] = timeclock[int(test[n][0].split(',')[1])] + test[n][1]
#timeclock[int(test[n][0].split(',')[1])]为timeclock[x]的值
counttimes[int(test[n][0].split(',')[1])] = timeclock[int(test[n][0].split(',')[1])]#在空字典中新增字典元素key=value
#int(test[n][0].split(',')[1])为timeclock[x]的x
print timeclock#为24个时间段
print counttimes#打印24个时间段对应的次数信息
root@kali:~/python/laowangpy/datadig#
python运行情况:
root@kali:~/python/laowangpy/datadig# python 720121103fre.py
[13, 3, 10, 5, 7, 13, 10, 7, 3, 3, 7, 6, 7, 11, 2, 5, 9, 1, 1, 6, 4, 3, 17, 3]
{0: 13, 1: 3, 2: 10, 3: 5, 4: 7, 5: 13, 6: 10, 7: 7, 8: 3, 9: 3, 10: 7, 11: 6, 12: 7, 13: 11, 14: 2, 15: 5, 16: 9, 17: 1, 18: 1, 19: 6, 20: 4, 21: 3, 22: 17, 23: 3}
root@kali:~/python/laowangpy/datadig#