我有一个这样的列表:名字,姓氏,赢,输
alex,kop,5,6
derex,拖把,0,11
sas,热,11,0
里奇,约翰,2,9
杰里米,甘斯,1,10分
我的程序在排行榜中显示文本文件中的数据,只显示至少赢了一场比赛的玩家,然后通过将他们的胜利乘以3来计算他们的分数。
def points():
template = "|{0:<30}|{1:<30}|{2:<30}|{3:<30}|{4:<30}"
header = template.format("1st name: ","2nd Name: ", "won: ", "lost: ","points: ")
print(header)
with open(r'prac2.txt', 'r') as file:
for line in file:
data = line.strip().split(',')
if int(data[2]) >= 1:
points = int(data[2]) * 3
data.append(points)
print(template.format(*data),'\n')我想从最高点数到最低点数对排行榜进行排序,但我不知道怎么做。
发布于 2017-03-14 21:34:09
您可以将内置的sorted与自定义密钥函数一起使用:
sorted(players, key=lambda player: player.wins * 3 + player.draws)PS:你应该保存绘图,如果可能发生的话;)
https://stackoverflow.com/questions/42787306
复制相似问题