如果点是sam,则在足球表中打印相同的位置

2024-09-28 19:31:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个足球表程序的工作,用户应该能够写一些分数,然后打印一个表格。所有球队和他们的数据(比如他们打了多少场比赛,得了多少分等等)都是列表中的对象。这个列表是按团队在打印出来之前的点数排序的。如果点是相同的,它会按其他一些东西排序,最后按它们的名字命名:

teamData.sort(key = lambda x: (-x.points, -x.goalDifference, -x.goalsForward, x.name))

这和我想要的一模一样。当我打印列表时,我是这样做的:

position = 1
for team in teamData:
    print(position, end = ": ")
    print(team)
    position += 1

teamData是存储所有团队对象的变量)

输出可以如下所示:

    1: TeamA,1,1,0,0,2,0,2,3
    2: TeamB,1,1,0,0,2,0,2,3
    3: TeamC,1,0,1,1,0,0,0,1

问题是(在本例中)teamAteamB具有完全相同的数据,因此,我希望输出如下所示:

    1: TeamA,1,1,0,0,2,0,2,3
    1: TeamB,1,1,0,0,2,0,2,3
    3: TeamC,1,0,1,1,0,0,0,1

Tags: 数据对象用户程序列表排序position团队
1条回答
网友
1楼 · 发布于 2024-09-28 19:31:17

记住前一组:

old_position, old_team = 0, None
for position, team in enumerate(teamData, 1):
    # I use enumerate() so I can change the position variable.

    if old_position and '''old_team and team compare to the same rank''':
        position = old_position
    else:
        old_position, old_team = position, team

    print(position, team, sep=': ')

相关问题 更多 >