# Dictionary is a series of key-value pairs. Each key is associated with a value, the key may be used to access the value associated therewith. Values can be numbers, strings, lists, and dictionaries. In fact, any object can be used as the value python dictionary. such as:
"" "Color point and is a bond, green, and 5 is a value between the key and value colon:." ""
alien_0={
"color":"green",
"points":5,
}
# To access the value of the dictionary, such as:
print(alien_0["color"])
print(alien_0["points"])
# Add a dictionary key-value pairs, such as:
alien_0["x_position"]=0
alien_0["y_position"]=25
print(alien_0)
# Modify the value of the dictionary, such as:
print ( "color before color alien_0 key dictionary is:" + alien_0 [ "color"])
alien_0["color"]="yellow"
print ( "color after color dictionary alien_0 key modification is:" + alien_0 [ "color"])
# Use del delete key-value pairs, you must specify the name of the dictionary and you want to remove the key. Note: Delete keys to disappear forever, can not be recovered. such as:
del alien_0["color"]
print(alien_0)
# All keys to traverse the dictionary of the method: the dictionary name .items (), for example:
user_0={
"username":"rose",
"first":"jack",
"second":"tony",
"last":"rose",
}
for key,value in user_0.items():
print("\n键:"+key)
print("值:"+value)
# All keys to scroll through the dictionary, the method: the dictionary name .keys (), for example:
for key in user_0.keys():
print("键:"+key)
# In order to traverse the dictionary all the keys, such as:
for key in sorted(user_0.keys()):
print ( "ordering of the keys:" + key)
# Iterate all the values of the dictionary method: the dictionary name .values (), for example:
for value in user_0.values():
print("值:"+value)
# To eliminate duplicate values, using set () set to ensure that the key values are unique is impossible to repeat the []. such as:
for value in set( user_0.values()):
print ( "eliminate duplicate values:" + value)
# Nested: Dictionaries list, such as:
alien_0={"color":"green","points":6}
alien_1={"color":"yellow","points":10}
alien_2={"color":"red","points":16}
aliens=[alien_0,alien_1,alien_2]
for alien in aliens:
print(alien)
# Nested: The list is stored in the dictionary, such as: S
pizza={
"crust":"thick",
"toppings":["mushroom","extra cheese"]
}
for topping in pizza["toppings"]:
print(topping)
# Nested: the dictionary is stored in the dictionary, such as:
users={
"rose":{
"sex":"女",
"age":"22",
"location":"American",
},
"jack":{
"sex":"男",
"age":"23",
"location":"American",
},
}
for key,value in users.items():
( "Information:" key +) print
print("性别:"+value["sex"]+",年龄:"+value["age"]+",所属地:"+value["location"])