alien_0={"color": "green"}
print(alien_0["color"])
alien_0={"color": "green", "points": 5}
new_points = alien_0["points"]
print("You just earn "+str(new_points)+" points !")
alien_0={"color": "green", "points": 5}
print(alien_0)
alien_0["x_positions"] = 0
alien_0["y_positions"] = 25
print(alien_0)
alien_0={"color": "green"}
print(alien_0["color"])
alien_0["color"] = "yellow"
print(alien_0["color"])
alien_0={"color": "green", "points": 5}
print(alien_0)
del alien_0["color"]
print(alien_0)
favorite_languages = {
"jen": "python",
"sarah": "c",
"edward": "ruby",
"phil": "python",
}
print("Sarah's favorite language is "+favorite_languages["sarah"].title()
+"!")
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())
favorite_languages = {
"jen": "python",
"sarah": "c",
"edward": "ruby",
"phil": "python",
}
for language in favorite_languages.values():
print(language.title())
favorite_languages = {
"jen": "python",
"sarah": "c",
"edward": "ruby",
"phil": "python",
}
for language in set(favorite_languages.values()):
print(language.title())
- 嵌套—有时候需要讲一系列字典存储在列表中,或将列表作为值储存在字典中
alien_0 = {"color": "green", "points": 5}
alien_1 = {"color": "yellow", "points": 10}
alien_2 = {"color": "red", "points": 15}
aliens = [alien_0,alien_1,alien_2]
for alien in aliens:
print(alien)
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)
users = {
"aeinstein": {
"first": "albert",
"last": "einstein",
"location": "princeton",
},
"mcurie": {
"first": "marie",
"last": "curie",
"location": "paris",
},
}
for user_name,user_info in users.items():
print("\nUsername: "+user_name)
full_name = user_info["first"] +" "+user_info["last"]
location = user_info["location"]
print("\t Full name: "+full_name)
print("\t Location: "+location)