Python 通过迭代分离列表中的列表

Python 通过迭代分离列表中的列表,python,list,Python,List,首先,这是一个家庭作业,但我已经做了一个星期了,没有什么进展。这个函数的目标是获取一个列表(每个列表包含一个足球运动员的数据),并根据球员所属的球队将列表分开。我还想把每个球员的数据加起来,这样我就可以得到每个球队的一份名单,所有球员的数据加起来 这是我到目前为止的代码。我目前遇到的问题是,有些团队每次都会使用不同的数据打印多次。否则,它似乎工作正常。此外,我们还受到限制,不允许使用类 def TopRushingTeam2010(team_info_2010): #running into t

首先,这是一个家庭作业,但我已经做了一个星期了,没有什么进展。这个函数的目标是获取一个列表(每个列表包含一个足球运动员的数据),并根据球员所属的球队将列表分开。我还想把每个球员的数据加起来,这样我就可以得到每个球队的一份名单,所有球员的数据加起来

这是我到目前为止的代码。我目前遇到的问题是,有些团队每次都会使用不同的数据打印多次。否则,它似乎工作正常。此外,我们还受到限制,不允许使用类

def TopRushingTeam2010(team_info_2010): #running into trouble calculating the rusher rating for each team, it also prints out the same team multiple times but with different stats. And just not getting the right numbers and order. 
    total_yards = 0
    total_TD = 0
    total_rush = 0
    total_fum = 0
    #works mostly, but is returning some teams twice, with different stats each time, which
    #should not be happening. so... yeah maybe fix that?
    for item in team_info_2010:
        team = item[0]
        total_yards = item[2]
        total_TD = item[3]
        total_rush = item[1]
        total_fum = item[4]
        new_team_info_2010.append([team, total_yards, total_TD, total_rush, total_fum])

        for other_item in team_info_2010:
            if other_item[0] == team:
                new_team_info_2010.remove([team, total_yards, total_TD, total_rush, total_fum])
                total_yards = total_yards + other_item[2]
                total_TD = total_TD + other_item[3]
                total_rush = total_rush + other_item[1]
                total_fum = total_fum + other_item[4]
                new_team_info_2010.append([team, total_yards, total_TD, total_rush, total_fum])

关于我应该朝哪个方向走,或者我是否朝着正确的方向走,有什么帮助或提示吗?

一个可能的问题是,当您在列表中迭代时,您正在从
团队信息\u 2010
中删除。试着删除那行代码。我看不出您想要从
team\u info\u 2010
中删除的明确原因,当您在迭代对象时修改对象时,行为通常是未定义的。更具体地说,请尝试删除以下代码行:

team_info_2010.remove(item)

它仍然返回同一个团队多次提示:使用字典并在team_info_2010中迭代一次。没有理由把它变成一个n平方算法。