第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

給定一個元組中由 2 個團(tuán)隊組成的元組列表,返回一個按獲勝團(tuán)隊到失敗團(tuán)隊排序的列表

給定一個元組中由 2 個團(tuán)隊組成的元組列表,返回一個按獲勝團(tuán)隊到失敗團(tuán)隊排序的列表

慕運維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定義一個函數(shù),該函數(shù)返回團(tuán)隊的排序列表,例如winning_list(matches)["Team T", "Team D", "Team A", ...]我有一個輔助方法,可以找到2個特定團(tuán)隊之間的獲勝順序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但這種方法似乎無效。此外,只有當(dāng)他們有一個共同的對手團(tuán)隊時,它才會起作用。
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻(xiàn)1784條經(jīng)驗 獲得超7個贊

根據(jù)提供的有關(guān)如何對解決方案進(jìn)行排名的信息,可以:


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()


查看完整回答
反對 回復(fù) 2022-08-16
  • 1 回答
  • 0 關(guān)注
  • 87 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號