Python学习第六篇——字典中的键和值

 favorite_language ={
"jen":"python",
"sarah":"c",
"edward":"ruby",
"phil":"python",
}
for name in sorted(favorite_language.keys()):# sort by zimushunxu
print(name.title()+",thank you for taking the poll.")
print("\n")
for name in favorite_language.keys():
print(name.title()+",thank you for taking the poll.")
print("\n")
print("The folllowing languages have been mentioned:")
for language in favorite_language.values():
print(language.title()) print("\nthe following languages have been mentioned:")
for language in set(favorite_language.values()):
print(language.title())

本程序的目的在于访问字典中的键和值.

1 访问键可以用.keys()的方式,实际上,默认的就是访问键,比如 name in favorite_language和 name in favorite_language.keys()中的name 都表示了键

2 访问值可以用.values()类

3 sorted()可以对特定的变量以特定的方式排序,比如如果是数字,以大小的顺序排序,如果是字符串,以首字母出现的顺序排序

上一篇:Java 面试题 —— java 源码


下一篇:杭电ACM2098--分拆素数和