Dictionary Dict
Set the dictionary to
DIC = {"name":"andraw"}
A, increase
1, increase the key value pair, increase the key value to Address:guangdong in the dictionary dic
dic["address""Guangdong"
2, increase by SetDefault method, add key value to Address:guangdong in dictionary dic
Dic.setdefault ("address","Guangdong")
3. Difference between the two
Method One, if the dictionary already exists the key value, then directly overwrite, there is no key value, then establish; method two, if the dictionary already exists key value, do not operate, if not exist, then establish
B, delete
1, delete by pop method, delete address key value pair in dictionary, return corresponding values
Dic.pop ("address")
Note: If not present, error. Here's how to fix it:
Dic.pop ("address", None)
Note: If the key value does not exist, return none, you can set the return value
2. Delete Via del
del dic["address"]
3, delete by Popitem, random Delete, return deleted Ganso, if empty error
Dic.popitem ()
4. Clear the Dictionary by clear method
Dic.clear ()
C, find
1, through the key value lookup, no error
value=dic["name"] print(value)
2, by the Get method to find, return values, not return none, can set the return value
Dic.get ("name")
D. Change
1. Direct change via key value pair
dic["name"]="andraw"
2, through the Update method changes, if DIC does not exist Dic2 key value pair, then dic increases the key value of Dic2, if present, the key value corresponding to the values of the value replacement
Dic1 = {"name":"aaa"} dic.update (DIC)
Python Learning (v)