Below python Code display python Common operations of Chinese dictionary , The dictionary is python It plays an important role in development , It is very important to master dictionary operation
# Create an empty dictionary
x = {}
Create a dictionary with three items
x = {"one":1, "two":2, "three":3}
Access one of these elements
x['two']
Returns a list of all keys in the dictionary
x.keys()
Returns a list of all values in the dictionary
x.values()
Add a new project
x["four"]=4
Modify a dictionary item
x["one"] = "uno"
Delete a dictionary entry
del x["four"]
Copy a dictionary to the new variable
y = x.copy()
Clear all dictionary entries
x.clear()
Return dictionary length , Number of projects
z = len(x)
Checks whether the dictionary contains the specified key
z = x.has_key("one")
Traverse the dictionary key
for item in x.keys(): print item
Traverse the list of values in the dictionary
for item in x.values(): print item
Use if Statement to obtain the corresponding key value in the dictionary
if "one" in x:
print x['one']
if "two" not in x:
print "Two not found"
if "three" in x:
del x['three']</pre>