我的代碼到目前爲止,但是當我試圖運行它。它給我提供了錯誤代碼ValueError: max() arg is an empty sequence
。ValueError:max()arg是一個空序列 - 排序文本文件中的數據
在文本文件中的數據排序是這樣的:
Kevin 2 3 6
Mary 8 6 6
Ollie 9 7 3
我的代碼dosent似乎字母順序這套並且或將其排序最高到最低得分。
from __future__ import division
from operator import attrgetter
class Player(object):
def __init__(self, name, scores):
self.name = name
self.scores = scores
self.highscore = max(scores)
self.avgscore = sum(scores)/len(scores)
with open('classFilea.txt') as f:
players = []
for line in f:
elems = line.split(',')
players.append(Player(elems[0], map(int, elems[1:])))
byhighscore = sorted(players, key=attrgetter('highscore'), reverse=True)
byavg = sorted(players, key=attrgetter('avgscore'), reverse=True)
print('')
for p in byhighscore:
print('{0} {1:g}'.format(p.name, p.highscore))
print('')
for p in byavg:
print('{0} {1:g}'.format(p.name, p.avgscore))
'ValueError:max()arg是一個空序列'表示某些行的'scores'爲空 –