我有这段代码:
def PlayerStats(self,LSTPlayers,actualround):
count = 0
statDict = defaultdict(list)
Stats = CStats.CStats('Cricket',self.Logs) #create instance
statDict = Stats.WritetoDict()
for objplayer in LSTPlayers:
if Stats.FindPlayerIndex(statDict,"Player",objplayer.PlayerName) == -1: #if there is no player in the stats csv file
self.Logs.Log("ERROR","Cannot find {} in the stats file! I will add them.".format(objplayer.PlayerName))
for key in statDict: count += 1
self.Logs.Log("ERROR","Count of keys is: {}".format(count))
statDict[count+1].append(objplayer.PlayerName,objplayer.GetTotalTouch,actualround,Stats.MPR(objplayer.PlayerName))
else:
self.Logs.Log("DEBUG","Updating {}'s stats!".format(objplayer.PlayerName))
我一直收到错误:IndexError:列表分配索引超出范围。
在第5行中,statDict正在使用另一个读取csv文件的类的defaultdict(list)进行更新,并返回列表字典。
第7行在字典中查找玩家名称,如果找不到,则返回-1,否则返回玩家的索引。
我希望在列表字典中添加一个新列表,如果播放器已经在那里,则会显示信息。
更新 - 这是我在CStats文件中的writetodict功能的代码:
def WritetoDict(self):
with open(self.pathfile) as f:
self.PlayerStatDict = [{k: str(v) for k, v in row.items()}
for row in csv.DictReader(f, skipinitialspace=True)]
return self.PlayerStatDict
PlayerStatDict被定义为CStats文件中的defaultdict(列表)。