gpt4 book ai didi

python - 如何打印每本词典的名称?

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:07 25 4
gpt4 key购买 nike

我这个学期刚刚开始编码,所以如果你能使用简单的方法来帮助我找到答案,我将不胜感激。基本上,我只是希望它打印每个字典的名称,然后列出它的内容。哦,你知道,我实际上什至不喜欢运动,这只是我之前想要改进的一项家庭作业。这是我所得到的,是的,我知道它不能按我想要的方式工作:

football = {
'favorite player': 'Troy Aikman',
'team': 'Dallas Cowboys',
'number': '8',
'position': 'quarterback'
}
baseball = {
'favorite player': 'Jackie Robinson',
'team': 'Brooklyn Dodgers',
'number': '42',
'position': 'second baseman'
}
hockey = {
'favorite player': 'Wayne Gretzky',
'team': 'New York Rangers',
'number': '99',
'position': 'center'
}

sports = [football, baseball, hockey]
my_sports = ['Football', 'Baseball', 'Hockey']
for my_sport in my_sports:
print(my_sport)
for sport in sports:
for question, answer in sport.items():
print(question.title + ": " + answer)
print("\n")

我想要打印:

Football
Favorite player: Troy Aikman
Team: Dallas Cowboys
Number: 8
Position: quarterback

Baseball:
Favorite player: Jackie Robinson
Team: Brooklyn Dodgers
Number: 42
Position: second baseman

...等等。我怎样才能达到我想要的结果?越简单越好,请使用Python 3,我对Python 2一无所知。

最佳答案

my_sports = {'Football': football, 'Baseball' : baseball, 'Hockey' : hockey}
for key,value in my_sports.items():
print(key)

for question, answer in value.items():
print(question + ": " + answer)
print("\n")

关于python - 如何打印每本词典的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46611196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com