【这是博主去年写的一直扔在草稿箱的文章...都不太有印象了(是我写的么?)...快来检查错误....】
笔试的时候,碰到一个题目,大概意思是有一个文件,比如下面这样的:
Log.txt
挑出带 '/bookmark/' 的URL,并且按照第二列的数字大小进行排序,取最大的三行数据
40:01:67:77 623 http://www.baidu.com/bookmark/0 2013-10-20-21-05-140
28:59:64:31 461 http://www.baidu.com/bookmark/1 2013-10-20-21-05-141
74:20:88:31 767 http://www.baidu.com/bookmark/2 2013-10-20-21-05-142
76:37:77:23 936 http://www.baidu.com/bookmark/3 2013-10-20-21-05-143
20:83:92:84 494 http://www.baidu.com/bookmark/4 2013-10-20-21-05-144
95:73:49:72 780 http://www.baidu.com/bookmark/5 2013-10-20-21-05-145
68:31:23:39 479 http://www.baidu.com/bookmark/6 2013-10-20-21-05-146
04:02:88:15 583 http://www.baidu.com/bookmark/7 2013-10-20-21-05-147
76:18:41:12 438 http://www.baidu.com/bookmark/8 2013-10-20-21-05-148
91:26:32:15 790 http://www.baidu.com/bookmark/9 2013-10-20-21-05-149
34:80:26:32 452 http://www.baidu.com/bookstore/0 2013-10-20-21-05-140
53:40:23:66 223 http://www.baidu.com/bookstore/1 2013-10-20-21-05-141
11:94:98:34 766 http://www.baidu.com/bookstore/2 2013-10-20-21-05-142
05:97:33:78 263 http://www.baidu.com/bookstore/3 2013-10-20-21-05-143
64:37:79:73 746 http://www.baidu.com/bookstore/4 2013-10-20-21-05-144
用下面的代码生成上面的txt文件做测试:
import time
import os
import random
上面是会用到的那啥,下面是生成文件代码:
f = open('Log.txt','w')
for each in range(10):
f.write(str(random.randint(0,9))+str(random.randint(0,9))+':'+str(random.randint(0,9))+str(random.randint(0,9))+':'+str(random.randint(0,9))+str(random.randint(0,9))+':'+str(random.randint(0,9))+str(random.randint(0,9))+' ')
f.write(str(random.randint(111,999))+' ')
f.write("http://www.baidu.com/bookmark/"+str(each)+' ')
f.write(time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime())+str(each)+'\n')
for each in range(5):
f.write(str(random.randint(0,9))+str(random.randint(0,9))+':'+str(random.randint(0,9))+str(random.randint(0,9))+':'+str(random.randint(0,9))+str(random.randint(0,9))+':'+str(random.randint(0,9))+str(random.randint(0,9))+' ')
f.write(str(random.randint(1111111111,9999999999))+' ')
f.write("http://www.baidu.com/bookstore/"+str(each)+' ')
f.write(time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime())+str(each)+'\n')
f.close()
窝是技术渣,只好这种死蠢的方法生成文件QAQ
咳,接下去开始处理数据:
results=[]
i=0
fp=open('Log.txt','r')
for each in fp.readlines():
if('/bookmark/' in each):
results.append(tuple(each.split()))
max=sorted(results,key=lambda result:result[1],reverse=True)
print(max[0:2])
fp.close()
嗯,看看结果:
[('76:37:77:23', '936', 'http://www.baidu.com/bookmark/3', '2013-10-20-21-05-143'), ('91:26:32:15', '790', 'http://www.baidu.com/bookmark/9', '2013-10-20-21-05-149'), ('95:73:49:72', '780', 'http://www.baidu.com/bookmark/5', '2013-10-20-21-05-145')]
哟西,Top 3粗来了~\(≧▽≦)/~
=。=因为笔试的时候没做出来,很不甘心……
感谢各种bo主的经验总结>.<