Python:每日一题 94(答题领鱼币)
要给班上同学的成绩排名次,规则如下:成绩从高往低排,如果有成绩相同的名次相同,但后续的名次要算前面的总人数。
比如,两个100分的都是第一名,紧接着的99分的是第三名,而不是第二名。
给出一个列表,是全班的成绩:
score = [('A', 80), ('B', 86), ('C', 92), ('D', 87), ('E', 88), ('F', 85),
('G', 86), ('H', 89), ('I', 88), ('J', 94), ('K', 94), ('L', 80),
('M', 83), ('N', 80), ('O', 88), ('P', 94), ('Q', 82), ('R', 84),
('S', 96), ('T', 100), ('U', 82), ('V', 81), ('W', 94), ('X', 81),
('Y', 89), ('Z', 92)]
要求最后打印出如下的结果:三列分别是名次,姓名和成绩
1 T 100
2 S 96
3 J 94
3 K 94
3 P 94
3 W 94
7 C 92
7 Z 92
9 H 89
9 Y 89
11 E 88
11 I 88
11 O 88
14 D 87
15 B 86
15 G 86
17 F 85
18 R 84
19 M 83
20 Q 82
20 U 82
22 V 81
22 X 81
24 A 80
24 L 80
24 N 80
我的解法:
**** Hidden Message ***** 本帖最后由 Noregret 于 2017-9-7 21:07 编辑
score = [('A', 80), ('B', 86), ('C', 92), ('D', 87), ('E', 88), ('F', 85),
('G', 86), ('H', 89), ('I', 88), ('J', 94), ('K', 94), ('L', 80),
('M', 83), ('N', 80), ('O', 88), ('P', 94), ('Q', 82), ('R', 84),
('S', 96), ('T', 100), ('U', 82), ('V', 81), ('W', 94), ('X', 81),
('Y', 89), ('Z', 92)]
a = sorted(score, key=lambda x:x)
a.reverse()
b = []
for each in a:
tuple1 = , each]
b.append(tuple1)
print(b)
for each in range(1, 27):
b.insert(0, each)
for c in range(1, 27):
if b == b:
b = b
print(b, b, b) def score():
score = [('A', 80), ('B', 86), ('C', 92), ('D', 87), ('E', 88), ('F', 85),
('G', 86), ('H', 89), ('I', 88), ('J', 94), ('K', 94), ('L', 80),
('M', 83), ('N', 80), ('O', 88), ('P', 94), ('Q', 82), ('R', 84),
('S', 96), ('T', 100), ('U', 82), ('V', 81), ('W', 94), ('X', 81),
('Y', 89), ('Z', 92)]
score.sort(key=lambda k: k, reverse=True)
flag = 1
score = score + (flag,)
for i in range(1,len(score)):
if score == score:
score = score + (score,)
flag += 1
else:
flag += 1
score = score + (flag,)
return score
if __name__ == '__main__':
score = score()
for i in range(len(score)):
print(score,score,score) score = [('A', 80), ('B', 86), ('C', 92), ('D', 87), ('E', 88), ('F', 85),
('G', 86), ('H', 89), ('I', 88), ('J', 94), ('K', 94), ('L', 80),
('M', 83), ('N', 80), ('O', 88), ('P', 94), ('Q', 82), ('R', 84),
('S', 96), ('T', 100), ('U', 82), ('V', 81), ('W', 94), ('X', 81),
('Y', 89), ('Z', 92)]
score.sort(key=lambda d:d,reverse=True)
index0 = 1
print('1',score,score)
for i in range(1,len(score)):
if(score==score):
print(index0,score,score)
else:
index0 = i+1
print(i+1,score,score)
运行结果:
1 T 100
2 S 96
3 J 94
3 K 94
3 P 94
3 W 94
7 C 92
7 Z 92
9 H 89
9 Y 89
11 E 88
11 I 88
11 O 88
14 D 87
15 B 86
15 G 86
17 F 85
18 R 84
19 M 83
20 Q 82
20 U 82
22 V 81
22 X 81
24 A 80
24 L 80
24 N 80
请按任意键继续. . . 本帖最后由 左手十字 于 2017-9-7 22:47 编辑
score = [('A', 80), ('B', 86), ('C', 92), ('D', 87), ('E', 88), ('F', 85),
('G', 86), ('H', 89), ('I', 88), ('J', 94), ('K', 94), ('L', 80),
('M', 83), ('N', 80), ('O', 88), ('P', 94), ('Q', 82), ('R', 84),
('S', 96), ('T', 100), ('U', 82), ('V', 81), ('W', 94), ('X', 81),
('Y', 89), ('Z', 92)]
def score_sorted(list_sc):
new_list = sorted(zip(dict(list_sc).values(),dict(list_sc).keys()),reverse= True)
number = 1
flog =1
list1=]
for score,name in new_list:
if score in list1:
print(number,name,score)
flog+=1
else:
number = flog
print(number,name,score)
list1.append(score)
flog+=1
score_sorted(score)
本帖最后由 ChrisYang 于 2017-9-7 21:43 编辑
e = [('A', 80), ('B', 86), ('C', 92), ('D', 87), ('E', 88), ('F', 85),
('G', 86), ('H', 89), ('I', 88), ('J', 94), ('K', 94), ('L', 80),
('M', 83), ('N', 80), ('O', 88), ('P', 94), ('Q', 82), ('R', 84),
('S', 96), ('T', 100), ('U', 82), ('V', 81), ('W', 94), ('X', 81),
('Y', 89), ('Z', 92)]
scores=[]#该列表只有成绩
for i in e:
score=i
scores.append(score)
names=[]#该列表只有名字
for i in e:
name=i
names.append(name)
score=list(zip(scores,names))
score.sort(reverse=True)#(成绩,名字)正序
lis=[]#(名次,名字,成绩)里面是元组形式
for i in range(1,len(e)+1):
lis.append(,score])
for i in range(1,len(e)):
if lis==lis:
lis=lis
for each in lis:
print(each,each,each)
————————————————————————————
运行结果
===================== RESTART: D:\Python36-32\caogao.py =====================
1 T 100
2 S 96
3 W 94
3 P 94
3 K 94
3 J 94
7 Z 92
7 C 92
9 Y 89
9 H 89
11 O 88
11 I 88
11 E 88
14 D 87
15 G 86
15 B 86
17 F 85
18 R 84
19 M 83
20 U 82
20 Q 82
22 X 81
22 V 81
24 N 80
24 L 80
24 A 80
>>> ChrisYang 发表于 2017-9-7 21:19
改过来了,刚刚发错了版本{:5_92:} 改过来了 本帖最后由 醉挽丶南风 于 2017-9-8 00:17 编辑
score = [('A', 80), ('B', 86), ('C', 92), ('D', 87), ('E', 88), ('F', 85),
('G', 86), ('H', 89), ('I', 88), ('J', 94), ('K', 94), ('L', 80),
('M', 83), ('N', 80), ('O', 88), ('P', 94), ('Q', 82), ('R', 84),
('S', 96), ('T', 100), ('U', 82), ('V', 81), ('W', 94), ('X', 81),
('Y', 89), ('Z', 92)]
score.sort(key=lambda item:item,reverse=True)
for i in range(len(score)):
if i>0:
if score!=score:
now=i+1
else:
now=i+1
print(now,score,score)
score.sort(key=lambda n: n, reverse=True)
for m,n in enumerate(score):
c = m+1 if n!=score else c
print(c,n,n) how happy to this {:10_319:}这个真不会 偷窥下
本帖最后由 schweinfan 于 2017-9-8 16:25 编辑
score = [('A', 80), ('B', 86), ('C', 92), ('D', 87), ('E', 88), ('F', 85),
('G', 86), ('H', 89), ('I', 88), ('J', 94), ('K', 94), ('L', 80),
('M', 83), ('N', 80), ('O', 88), ('P', 94), ('Q', 82), ('R', 84),
('S', 96), ('T', 100), ('U', 82), ('V', 81), ('W', 94), ('X', 81),
('Y', 89), ('Z', 92)]
rank = sorted(score, key = lambda e: e, reverse = True)
rank = (1,) + rank
temp = rank
print(rank, rank, rank)
for i in range(1,len(rank)):
if rank != temp:
rank = (i + 1,) + rank
temp = rank
else:
rank = (rank,) + rank
temp = rank
print(rank, rank, rank)
输出:
1 T 100
2 S 96
3 J 94
3 K 94
3 P 94
3 W 94
7 C 92
7 Z 92
9 H 89
9 Y 89
11 E 88
11 I 88
11 O 88
14 D 87
15 B 86
15 G 86
17 F 85
18 R 84
19 M 83
20 Q 82
20 U 82
22 V 81
22 X 81
24 A 80
24 L 80
24 N 80 先生成索引,再用字典把相同的分数的排名变成同一个
def fun(a):
lst = list(enumerate(sorted(a, key=lambda x: x,reverse=True), 1))
dic = {j:i for i,j in lst[::-1]}
for _,k in lst:
print(dic],*k) 有鱼币吗 score = [('A', 80), ('B', 86), ('C', 92), ('D', 87), ('E', 88), ('F', 85),
('G', 86), ('H', 89), ('I', 88), ('J', 94), ('K', 94), ('L', 80),
('M', 83), ('N', 80), ('O', 88), ('P', 94), ('Q', 82), ('R', 84),
('S', 96), ('T', 100), ('U', 82), ('V', 81), ('W', 94), ('X', 81),
('Y', 89), ('Z', 92)]
#冒泡法排序
for i in range(len(score)):
for j in range(i+1,len(score)):
if score < score:
score, score = score, score
#处理序号
for i in range(len(score)):
if score != score:
print(i+1,score,score)
n = i+1
else:
print(n,score,score) 本帖最后由 zyq940 于 2017-9-11 16:36 编辑
score = [('A', 80), ('B', 86), ('C', 92), ('D', 87), ('E', 88), ('F', 85),
('G', 86), ('H', 89), ('I', 88), ('J', 94), ('K', 94), ('L', 80),
('M', 83), ('N', 80), ('O', 88), ('P', 94), ('Q', 82), ('R', 84),
('S', 96), ('T', 100), ('U', 82), ('V', 81), ('W', 94), ('X', 81),
('Y', 89), ('Z', 92)]
score = sorted(score, key=lambda x:x)
score.reverse()
rank1 = 1
rank2 = 1
for i in range(0,25):
if score !=score and score != score:
print(rank1,score,score)
rank1 += 1
rank2 += 1
elif score == score:
print(rank1,score,score)
rank2 += 1
elif score !=score and score ==score:
print(rank1,score,score)
rank2 += 1
rank1 = rank2
print(rank1,score,score)
score = [('A', 80), ('B', 86), ('C', 92), ('D', 87), ('E', 88), ('F', 85),
('G', 86), ('H', 89), ('I', 88), ('J', 94), ('K', 94), ('L', 80),
('M', 83), ('N', 80), ('O', 88), ('P', 94), ('Q', 82), ('R', 84),
('S', 96), ('T', 100), ('U', 82), ('V', 81), ('W', 94), ('X', 81),
('Y', 89), ('Z', 92)]
sort = sorted(score, key=lambda student: student, reverse=True)
out = []
temp = {}
for index, student in enumerate(sort):
rank = temp.get(student, index + 1)
out.append((rank, *student))
temp] = rank
print('\n'.join('%s %s %s' % (n, n, n) for n in out)) score = [('A', 80), ('B', 86), ('C', 92), ('D', 87), ('E', 88), ('F', 85),
('G', 86), ('H', 89), ('I', 88), ('J', 94), ('K', 94), ('L', 80),
('M', 83), ('N', 80), ('O', 88), ('P', 94), ('Q', 82), ('R', 84),
('S', 96), ('T', 100), ('U', 82), ('V', 81), ('W', 94), ('X', 81),
('Y', 89), ('Z', 92)]
score.sort(key = lambda a : a,reverse = True)
index = 1
print(str(index),score,score)
for i in range(len(score)):
if score == score:
print(index+1,score,score)
else:
index = i + 1
print(index+1,score,score)
print(score)
score = [('A', 80), ('B', 86), ('C', 92), ('D', 87), ('E', 88), ('F', 85),
('G', 86), ('H', 89), ('I', 88), ('J', 94), ('K', 94), ('L', 80),
('M', 83), ('N', 80), ('O', 88), ('P', 94), ('Q', 82), ('R', 84),
('S', 96), ('T', 100), ('U', 82), ('V', 81), ('W', 94), ('X', 81),
('Y', 89), ('Z', 92)]
score.sort(key=lambda s:s,reverse=True)
i=0
n=0
for each in score:
if score>score:
print(i+1,score,score)
i=i+1
else:
n=i
print(n,score,score)
i=i+1