为了账号安全,请及时绑定邮箱和手机立即绑定

给定一个元组中由 2 个团队组成的元组列表,返回一个按获胜团队到失败团队排序的列表

给定一个元组中由 2 个团队组成的元组列表,返回一个按获胜团队到失败团队排序的列表

慕运维8079593 2022-08-16 16:14:57
我有一个元组列表:matches = [("Team D","Team A"), ("Team E","Team B"), ("Team T","Team B"), ("Team T","Team D"), ("Team F","Team C"), ("Team C","Team L"), ("Team T","Team F")]以第一个元组为例,因为元组中的 is 之前,胜过 。在2支球队不相互对抗的情况下,我们以这种方式确定获胜顺序:例如,如果我们想找出和之间的获胜顺序,因为获胜和获胜,整体获胜也是如此。("Team D", "Team A")DADATADATDTAT > D > A定义一个函数,该函数返回团队的排序列表,例如winning_list(matches)["Team T", "Team D", "Team A", ...]我有一个辅助方法,可以找到2个特定团队之间的获胜顺序def winner(matches, team_1, team_2):    size = len(matches)    lst1 = []    lst2 = []    for i in range(0, size): # extract games with team1        if matches[i][0] == team1 or matches[i][1] == team1:            lst1.append(matches[i])        elif matches[i][0] == team2 or matches[i][1] == team2: # extract games with team2            lst2.append(matches[i])    lst_partner1 = [] # opponent teams involving team1    lst_partner2 = [] # opponent teams involving team2    for i in range(0, len(lst1)):        if lst1[i][0] != team1:            lst_partner1.append(lst1[i][0])        elif lst1[i][1] != team1:            lst_partner1.append(lst1[i][1])    for i in range(0, len(lst2)):        if lst2[i][0] != team2:            lst_partner2.append(lst2[i][0])        elif lst2[i][1] != team2:            lst_partner2.append(lst2[i][1])    common = [value for value in lst_partner1 if value in lst_partner2] # opponent team that played against team1 and team2    # print(common)    opponent_team = common[0]    # print(opponent_team)    if len(common) == 0:        return 0    else:        for  i in range(0, len(lst1)):            if opponent_team in lst1[i]:                idx_opp1 = lst1[i].index(opponent_team)        for l in range(0, len(lst2)):            if opponent_team in lst2[l]:                idx_opp2 = lst2[l].index(opponent_team)        if idx_opp1 == idx_opp2:            return 0        elif idx_opp1 < idx_opp2:            return 2        elif idx_opp1 > idx_opp2:            return 1        else:            return 0但这种方法似乎无效。此外,只有当他们有一个共同的对手团队时,它才会起作用。
查看完整描述

1 回答

?
噜噜哒

TA贡献1784条经验 获得超7个赞

根据提供的有关如何对解决方案进行排名的信息,可以:


from collections import defaultdict


matches = [("Team D", "Team A"), ("Team E", "Team B"), ("Team T", "Team B"),

           ("Team T", "Team D"), ("Team F", "Team C"), ("Team C", "Team L"),

           ("Team T", "Team F")]



def winning_list(mathces):

    scores = defaultdict(int)

    for fst, snd in matches:

        scores[fst] += 1

        scores[snd] -= 1

    return sorted(scores.items(), key=lambda e: e[1], reverse=True)



ranking = winning_list(matches)

print(ranking)

为了使它更简单,我们可以使用collections.Counter


from collections import Counter



def winning_list2(mathces):

    scores = Counter()

    for fst, snd in matches:

        scores[fst] += 1

        scores[snd] -= 1

    return scores.most_common()


查看完整回答
反对 回复 2022-08-16
  • 1 回答
  • 0 关注
  • 72 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信