设文件score.txt中有如下的学生成绩:
Jim 80
Tom 75
Peter 85
Jim 60
Peter 78
Jim 100
其中学生数量在10-20之间,总行数在100-200之间,打印"name average(=总成绩/科目数)"如下形式(忘了是按成绩的升序还是降序了
):
Tom 75
Jim 80
Peter 80.5
应该说还是挺简单的,以下是两种接法:
awk '{score[$1]+=$2;num[$1]+=1};END{for (i in score){print i " " score[i]/num[i]} }' score.txt |sort -k2 (-r)
|
#!/usr/bin/env python
#file:score.py
import re
from operator import itemgetter
scorehash={}
numhash={}
f=open('./score.txt','r')
for i in f:
a=re.search(r'(\w*)\s*(\d*)',i)
key=a.group(1)
value=int(a.group(2))
#if not numhash.has_key(key):
if key not in numhash:
numhash[key]=0
#if not scorehash.has_key(key):
scorehash[key]=0
numhash[key]+=1
scorehash[key]+=value
#for j in scorehash.keys():
# print j + ' ' + str(scorehash[j]/numhash[j])
#python score.py|sort -k2 (-r)
#or use the below codes,
#for j in sorted(scorehash.items(),key=itemgetter(1),reverse=True):
for j in sorted(scorehash.items(), key=itemgetter(1)):
print j[0] +' '+str(scorehash[j[0]]/numhash[j[0]])
f.close()
|
阅读(1543) | 评论(0) | 转发(0) |