Python dictionary,
Python dictionary 1. Create a dictionary
Dictionary is another variable container model that can store any type of objects.
Each key-value (key => value) pair of the dictionary uses the colon (:), And each pair is separated by a comma (,The entire dictionary is included in curly brackets ({}). In addition, the key-value pairs in the dictionary are unordered. The order of key-value pairs may change.
The key must be unique, but the value is not required. Values can be of any data type, but keys must be unchangeable, such as strings, numbers, or tuples.
1. directly create
A = {'name': 'Tom ', age': 12, 'hobby': ['sing', timing], 'Number _ of_family ': {'father ': 'Dad ', 'Mother': 'Mom '}}
2. Create with Class 1) create with tuples
B = dict ('name', 'Tom '), ('age', 12) # create using tuples
Print (B) # {'name': 'Tom ', 'age': 12}
2) create with List
C = dict ([['name', 'Tom '], ['age', 12]) # create from List
print(c) #{'name': 'tom', 'age': 12}2. dictionary operations 1. Add 1) directly add
C = {'name': 'Tom ', 'age': 12} # create a dictionary c
C ['Mother '] = 'ms' # Add a ['Mother': 'Mom '] key-Value Pair
Print (c) # {'Mother ': 'ms', 'age': 12, 'name': 'Tom '}
2) use setdefault
Setdefault is the creation of the return value. It can return the value of the key that increases the key-value pair. When a key-value pair is created, the system checks whether the key-Value Pair exists in the original dictionary. You can only add one key-value pair at a time. You cannot add multiple key-value pairs at a time.
D = {'age': 12, 'name': 'Tom '} # create a dictionary d
Ret = d. setdefault ('Mother ', 'Mom') # Add {'Mother ': 'Mom '}
print(d) #{'name': 'tom', 'mother':'mom', 'age': 12}
print(ret) # mom
2. query 1) Direct Query
D = {'age': 12, 'name': 'Tom '} # create a dictionary d
Print (d ['name']) # tom
2) query all the key names in the dictionary
D = {'age': 12, 'name': 'Tom '} # create a dictionary d
Print (list (d. keys () # Use the class to create a list to list key names ['age', 'name']
3) display dictionary content in the form of list nested tuples
D = {'age': 12, 'name': 'Tom '} # create a dictionary d
Print (list (d. items () # [('age', 12), ('name', 'Tom ')]
4) retrieve all the key values in the dictionary
D = {'age': 12, 'name': 'Tom '} # create a dictionary d
Print (list (d. values () # ['Tom ', 'Mom', 12]
3. Change 1) assign a value again.
D = {'age': 12, 'name': 'Tom '} # create a dictionary d
D ['name'] = 'Jim '# change the key value of name to jim
print(d) # {'name': 'jim', 'age': 12}2) update
D = {'age': 12, 'name': 'Tom '} # create a dictionary d
E = {'Mother ': 'ms'} # create a dictionary e
D. update (e) # Add dictionary e to dictionary d
print(d) # {'name': 'jim', 'mother':'mom', 'age': 12}4. Delete 1) del
D = {'age': 12, 'name': 'Tom '} # create a dictionary d
Del d ['name'] # Delete the 'name' key-Value Pair
print(d) # {'age': 12}
Del d # Delete the entire dictionary
2) clear
D = {'age': 12, 'name': 'Tom '} # create a dictionary d
D. clear () # clear the dictionary d
Print (d )#{}
3) pop
Pop can return the key value for deleting a key-value pair.
D = {'age': 12, 'name': 'Tom '} # create a dictionary d
Res = d. pop ('name') # Delete the 'name' key-Value Pair
Print (res) # output the 'Tom 'key value for deleting the key-Value Pair'
Print ('d = ', d) # {'age': 12}
5. Sort sorted
Sorted () can rearrange the numbers by size, or sort the letters in the order of the 26 English letters before the lowercase letters. Strings are listed in the first letter. (In essence, it is sorted in ascending order by ASCII code, that is, numbers <upper-case letters <lower-case letters)
1) sort by key name size
When creating a dictionary, the key name can be a string (str) or an integer (int), but the two cannot appear in the same dictionary at the same time. Otherwise, an error (TypeError: unorderable types: int () <str ()).
D = {'B': '1', 'A': '2', '3': 'A', '1': 'B', 'D ': '5'} # create a dictionary d
Print (d) # {'3': 'A', 'A': '2', '1': 'B', 'D': '5 ', 'B': '1 '}
Print ('s = ', sorted (d. items () # s = [('1', 'B'), ('3', 'A'), ('D', '5 '), ('A', '2'), ('B', '1')]
2) Output key values by size
When creating a dictionary, the key value can be a string (str) or an integer (int), but the two cannot appear in the same dictionary at the same time. Otherwise, an error (TypeError: unorderable types: int () <str ()).
D = {'B': '1', 'A': '2', '3': 'A', '1': 'B', 'D ': '5'} # create a dictionary d
Print (d) # {'3': 'A', 'A': '2', '1': 'B', 'D': '5 ', 'B': '1 '}
Print ('s = ', sorted (d. values () # s = ['1', '5', 'A', 'A',' B ']
6. dictionary Traversal
D = {'B': '1', 'A': 'A', '3': 'A', '1': 'B', 'D ': '5'} # create a dictionary d
For I in d: # Start traversing
print(i,d[i]) # 3 a
1 b
b 1
a A
D 5
Method 2:
This method is slow if the dictionary content is large.
D = {'B': '1', 'A': 'A', '3': 'A', '1': 'B', 'D ': '5'} # create a dictionary d
For I, v in d. items (): # Start Traversal
print(i,v) # 3 a
1 b
b 1
a A
D 5