簡體   English   中英

從 Python 中的嵌套字典中按順序排序和打印鍵和值

[英]Sort and print sequentially keys and values from a nested dictionary in Python

這是我在這里的第一個問題。 我正在學習如何在 Python 中操作字典。 但我正面臨挑戰。

我有一個足球比賽的嵌套字典。 每個子詞典是兩支球隊之間的一場比賽及其相應的進球數。 我需要打印每場比賽的結果,按字母順序對每個團隊進行排序。 此外,下一步是打印以下結果

比賽,但也包括上一場比賽的結果,保持所有球隊按字母順序排序。

下面是一個例子:

soccer = {
    1 : {"England": 2, "Poland": 3},
    2 : {"Italy": 2, "Sweeden": 0},
    3 : {"Spain": 2, "Belgium": 2}
}

輸出應該是這樣的:

Points Scored After Match 1

England: 0 points
Poland: 3 points


Points Scored After Match 2

England: 0 points
Italy: 3 points
Poland: 3 points
Sweeden: 0 points


Points Scored After Match 3

Belgium: 1 point
England: 0 points
Italy: 3 points
Poland: 3 points
Spain: 1 point
Sweeden: 0 points

提前致謝。

您可以使用此示例如何對字典中的鍵/值進行排序並為打印功能設置項目的格式:

soccer={1:{"England":2,"Poland":3}, 2:{"Italy":2, "Sweeden":0}, 3:{"Spain":2, "Belgium":2}}

points = {}
for match_no, match in sorted(soccer.items(), key=lambda k: k[0]):
    team_1, team_2 = match.keys()
    score_1, score_2 = match.values()

    if score_1 == score_2:
        points[team_1] = points.get(team_1, 0) + 1
        points[team_2] = points.get(team_2, 0) + 1
    elif score_1 > score_2:
        points[team_1] = points.get(team_1, 0) + 3
        points[team_2] = points.get(team_2, 0) + 0
    else:
        points[team_1] = points.get(team_1, 0) + 0
        points[team_2] = points.get(team_2, 0) + 3

    # print results:
    print()
    print('Points Scored After Match {}'.format(match_no))
    print()

    for team, p in sorted(points.items(), key=lambda k: k[0]):
        print('{}: {} point{}'.format(team, p, '' if p == 1 else 's'))

印刷:

Points Scored After Match 1

England: 0 points
Poland: 3 points

Points Scored After Match 2

England: 0 points
Italy: 3 points
Poland: 3 points
Sweeden: 0 points

Points Scored After Match 3

Belgium: 1 point
England: 0 points
Italy: 3 points
Poland: 3 points
Spain: 1 point
Sweeden: 0 points

這應該做:

soccer={1:{"England":2,"Poland":3}, 2:{"Italy":2, "Sweeden":0}, 3:{"Spain":2, "Belgium":2}}
points={"England":0,"Poland":0,"Italy":0, "Sweeden":0,"Spain":0, "Belgium":0}
for match_number in soccer:
    for country in soccer[match_number]:
        team_one, team_two = soccer[match_number].keys()
        score_one, score_two = soccer[match_number].values()
        if score_one > score_two:
            points[team_one] = points.get(team_one) + 3
        if score_one < score_two:
            points[team_two] = points.get(team_two) + 3
        if score_one == score_two:
            points[team_one] = points.get(team_one) + 1
            points[team_two] = points.get(team_two) + 1
        print("Points scored after: ", match_number)
        for key in points:
            print(key,points[key])

它輸出這個:

Points scored after:  1
England 0
Poland 3
Italy 0
Sweeden 0
Spain 0
Belgium 0
Points scored after:  1
England 0
Poland 6
Italy 0
Sweeden 0
Spain 0
Belgium 0
Points scored after:  2
England 0
Poland 6
Italy 3
Sweeden 0
Spain 0
Belgium 0
Points scored after:  2
England 0
Poland 6
Italy 6
Sweeden 0
Spain 0
Belgium 0
Points scored after:  3
England 0
Poland 6
Italy 6
Sweeden 0
Spain 1
Belgium 1
Points scored after:  3
England 0
Poland 6
Italy 6
Sweeden 0
Spain 2
Belgium 2

下次也嘗試發布您對問題的嘗試。 干杯!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM