1 favorite_language ={ 2 "jen":"python", 3 "sarah":"c", 4 "edward":"ruby", 5 "phil":"python", 6 } 7 for name in sorted(favorite_language.keys()):# sort by zimushunxu 8 print(name.title()+",thank you for taking the poll.") 9 print(" ") 10 for name in favorite_language.keys(): 11 print(name.title()+",thank you for taking the poll.") 12 print(" ") 13 print("The folllowing languages have been mentioned:") 14 for language in favorite_language.values(): 15 print(language.title()) 16 17 print(" the following languages have been mentioned:") 18 for language in set(favorite_language.values()): 19 print(language.title())
本程序的目的在于访问字典中的键和值.
1 访问键可以用.keys()的方式,实际上,默认的就是访问键,比如 name in favorite_language和 name in favorite_language.keys()中的name 都表示了键
2 访问值可以用.values()类
3 sorted()可以对特定的变量以特定的方式排序,比如如果是数字,以大小的顺序排序,如果是字符串,以首字母出现的顺序排序