在Python中对嵌套字典中的键和值进行排序并按顺序打印
这是我的第一个问题。我正在学习如何用Python操作字典。但我面临着一个挑战 我有一本嵌套的足球比赛词典。每个子词典都是一场比赛,两支球队和相应数量的进球。 我需要打印每场比赛的结果,按字母顺序对每个队进行排序。此外,下一步是打印以下结果 比赛,但也包括上一场比赛的结果,保持所有球队按字母顺序排序 以下是一个例子:在Python中对嵌套字典中的键和值进行排序并按顺序打印,python,loops,sorting,dictionary,Python,Loops,Sorting,Dictionary,这是我的第一个问题。我正在学习如何用Python操作字典。但我面临着一个挑战 我有一本嵌套的足球比赛词典。每个子词典都是一场比赛,两支球队和相应数量的进球。 我需要打印每场比赛的结果,按字母顺序对每个队进行排序。此外,下一步是打印以下结果 比赛,但也包括上一场比赛的结果,保持所有球队按字母顺序排序 以下是一个例子: soccer = { 1 : {"England": 2, "Poland": 3}, 2 : {"Italy&qu
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