简体   繁体   English

使用字典中的元组跟踪位置(Python 3)

[英]Tracking positions using tuples in a dictionary (Python 3)

I've implemented a dictionary/hashing algorithm to add the points to each team that plays (t1, t2 represents teams, p1, p2 represents their respective points per match). 我已经实现了字典/哈希算法,将得分添加到每个参与比赛的团队(t1,t2代表团队,p1,p2代表每场比赛各自的得分)。 3500 points are added if an away team wins, 3000 if a home team wins, 1000 if they tie, 50 if they lose. 如果客队获胜,则增加3500点;如果主队获胜,则增加3000点;如果并列,则增加1000;如果输掉,则增加50。 My code does just that, however, I get errors when two teams tie for first place or last place on the final leaderboard. 我的代码就是这样做的,但是,当两个团队并列最终排行榜的第一名或最后一名时,我会出错。 My mentors have suggested using tuples to keep a "counter" that tracks who made it to first place or last place first, however, due to tuples not being mutable, I don't know how to approach this without increasing the runtime of my program. 我的导师建议使用元组保留一个“计数器”,以跟踪谁使它排在首位或最后一位,但是,由于元组不可变,我不知道如何在不增加程序运行时间的情况下进行处理。 Any ideas? 有任何想法吗?

import sys

def firstLast(q):

     games = {}
     for i in range(q):
        t1, t2, p1, p2 = input().strip().split(' ')
        t1, t2, p1, p2 = [t1, t2, int(p1), int(p2)]

        if t1 not in games:
            games[t1] = p1
        else:
            games[t1] += p1

        if t2 not in games:
            games[t2] = p2
        else:
            games[t2] += p2

        if p1>p2:
            games[t1]+=3000
            games[t2]+=50
        elif p1==p2:
            games[t1]+=1000
            games[t2]+=1000
        elif p1<p2:
            games[t1]+=50
            games[t2]+=3500

    #first = max(games.values())
    #last = min(games.values())
    first = max(games, key=games.get)
    last = min(games, key=games.get)

    print (first,last,sep='\n')


if __name__ == "__main__":
    q = int(input())
    result = firstLast(q)

Here's a rework that prints all teams that tied for first or last instead of randomly displaying one: 这是一份重做,显示了排在第一名或最后一名并列的所有团队,而不是随机显示一个团队:

from collections import defaultdict

def firstLast(q):

    games = defaultdict(int)

    for _ in range(q):
        t1, t2, p1, p2 = input().strip().split(' ')
        p1, p2 = int(p1), int(p2)

        games[t1] += p1
        games[t2] += p2

        if p1 > p2:
            games[t1] += 3000
            games[t2] += 50
        elif p1 < p2:
            games[t1] += 50
            games[t2] += 3500
        else:
            games[t1] += 1000
            games[t2] += 1000

    maximum = max(games.values())
    minimum = min(games.values())

    first = [key for key, value in games.items() if value == maximum]
    last = [key for key, value in games.items() if value == minimum]

    print(' '.join(first), ' '.join(last), sep='\n')

if __name__ == "__main__":
    q = int(input())
    firstLast(q)

However, it's not exactly what you desire. 但是,这并不完全是您想要的。 My problem is I can't get my head around the concept of "the team that came in last place first". 我的问题是我无法理解“首先进入最后一位的团队”的概念。 Perhaps if you supplied some examples, it might help me and/or others answer your question. 也许如果您提供了一些示例,它可能会帮助我和/或其他人回答您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM