遍历所有的键—值对
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}
for key, value in user_0.items():
print("\nKey: " + key)
print("Value: " + value)
遍历字典中的所有键
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
for name in favorite_languages.keys():
print(name.title())
按顺序遍历字典中的所有键
for name in sorted(favorite_languages.keys()):
print(name.title())
遍历字典中的所有值
for language in favorite_languages.values():
print(language.title())
遍历字典中所有值时剔除重复项
for language in set(favorite_languages.values()):
print(language.title())