在字典内循环



我在字典中嵌套一个列表。我已经尝试循环它,但是输出显示的是每个字母而不是整个单词,并且它没有显示列表中的其他值。

favorite_languages = {
'jen': ['python', 'ruby'],
'sarah': ['c'],
'edward': ['ruby', 'go'],
'phil': ['python', 'haskell'],
}
for name,languages in favourite_languages.items():
print("n" + name.title() + "'s fav lang are:")
for language in languages:
print("t" + language.title())

输出是-

Jen's fav lang are:
P
Y
T
H
O
N

修复标识和小错别字后,您的代码为我工作:

上网试试!

favourite_languages = {
"jen": ["python", "ruby"],
"sarah": ["c"],
"edward": ["ruby", "go"],
"phil": ["python", "haskell"],
}
for name, languages in favourite_languages.items():
print("n" + name.title() + "'s fav lang are:")
for language in languages:
print("t" + language.title())

输出:

Jen's fav lang are:
Python
Ruby
Sarah's fav lang are:
C
Edward's fav lang are:
Ruby
Go
Phil's fav lang are:
Python
Haskell

相关内容

  • 没有找到相关文章

最新更新