- 论坛徽章:
- 0
|
#!/usr/bin/env python
#得分排名
import random;
def score_rank(scores):
ranks = [0] * 102
for score in scores:
ranks[score] += 1
ranks[101] = 1
for i in range(len(ranks)-2,0,-1):
ranks += ranks[i+1]
return ranks;
if __name__ == "__main__":
#create a random score list
scores = []
for x in range(40):
scores.append(random.randint(1,100))
ranks = score_rank(scores)
#print result
scores.sort()
for x in scores:
print("分数:{0}\t排名:{1}".format(x,ranks[x+1]))
执行结果:
>>>
分数:4 排名:40
分数:7 排名:39
分数:10 排名:38
分数:12 排名:37
分数:13 排名:36
分数:21 排名:35
分数:22 排名:34
分数:25 排名:33
分数:30 排名:32
分数:31 排名:31
分数:32 排名:30
分数:35 排名:29
分数:36 排名:27
分数:36 排名:27
分数:37 排名:24
分数:37 排名:24
分数:37 排名:24
分数:41 排名:23
分数:42 排名:22
分数:44 排名:21
分数:45 排名:19
分数:45 排名:19
分数:47 排名:18
分数:50 排名:17
分数:51 排名:16
分数:55 排名:15
分数:61 排名:14
分数:67 排名:13
分数:68 排名:12
分数:69 排名:11
分数:72 排名:10
分数:73 排名:9
分数:74 排名:8
分数:75 排名:7
分数:76 排名:6
分数:83 排名:5
分数:85 排名:4
分数:93 排名:2
分数:93 排名:2
分数:95 排名:1
>>>
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/23094/showart_2089686.html |
|