其他分享
首页 > 其他分享> > 有趣的题001

有趣的题001

作者:互联网

zhangsan 98
lisi 25
wangwu 34
zhangsan 76
zhangsan 36
zhangsan 54
lisi 68
zhangsan 66
lisi 25
zhangsan 83
lisi 99
wangwu 76
zhuliu 87
需求的把每个人的多次成绩取平均值,并输出有几次成绩
按成绩从高到低输出
结果类似这样
[‘zhangsan’, 68.83333333333333, 6]
[‘wangwu’, 55.0, 2]
[‘lisi’, 54.25, 4]
[‘zhuliu’, 8.0, 1]

def qsort(L):
    if len(L) <= 1: return L
    return qsort([lt for lt in L[1:] if lt[1] > L[0][1]]) + [L[0]] + qsort([ge for ge in L[1:] if ge[1] <= L[0][1]])


def xxx(fp):
    reads = fp.readlines()
    fp.close()

    for mm in range(1):

        all = {}
        for i in range(len(reads)):
            old = reads[i][:-1].split()
            if old[0] not in all.keys() :
                all[old[0]] = old[1:]+[1]
            else:
                all[old[0]]  = [(int(all[old[0]][0])+int(old[1])),all[old[0]][1]+1]
        end = []
        for i,j in all.items():
            j = [int(j[0])/j[1],j[1]]
            all[i]=j
            end.append([i]+j)
        e_end = qsort(end)
        for i in e_end:
            print i
            pass


t1 = time.time()
xxx( fp = open('/Users/zijiawang/Downloads/test', 'r'))
t2 =  time.time()

print ''
print t2-t1

冒泡法 和 快排,130万行数据都需要3.5秒左右。 看来优化还得从结构上优化。

 

标签:25,qsort,ge,wangwu,lisi,001,有趣,zhangsan
来源: https://blog.51cto.com/u_15282986/2952274