user_0 ={'username':'feven','first':'hw','last':'h'}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():#keys()返回一个列表,其中包含字典中所有键print(name.title())for name in favorite_languages:print(name.title())
按顺序遍历字典中的所有键
favorite_languages ={'jen':'python','sarah':'c','edward':'ruby','phil':'python'}for name insorted(favorite_languages.keys()):print(name.title()+", thank you for taking the poll.")
遍历字典中的所有值
favorite_languages ={'jen':'python','sarah':'c','edward':'ruby','phil':'python'}print("The following languages have been mentioned:")for language in favorite_languages.values():#没有考虑重复项print(language.title())print('-'*20)for language inset(favorite_languages.values()):#使用set()剔除重复项print(language.title())
#使用字典存储顾客点的披萨信息
pizza ={'crust':'thick','toppings':['mushrooms','extra cheese']}print("You ordered a "+ pizza['crust']+"crust pizza "+"with the following toppings:")for topping in pizza['toppings']:print("\t"+topping)#使用字典存储喜欢的编程语言
favorite_languages ={'jen':['python','ruby'],'sarah':['c'],'edward':['ruby','go'],'phil':['python','haskell']}for name, languages in favorite_languages.items():print("\n"+ name.title()+"'s favorite languages are:")for language in languages:print("\t"+ language.title())