The basic concept of the python dictionary, properties and CRUD
Basic concepts dictionary
1.1 dictionary definition
Another variable is the dictionary container model, and can store any type of object.
Each dictionary key (key => value) of the colon (:) divided, (,) between each pair separated by commas, including whole dictionary in curly braces ({}) in the following format :
d = {key1 : value1, key2 : value2 }
1.2 dictionary creation
Key must be unique, but the value is not necessary.
Value may take any data type, but the key must be immutable, such as strings, numbers, or tuples.
A simple example dictionary:
s = {
'linux':[100,99,89],
'python':[90,99,100]
}
print(s,type(s))
Output:
So also create a dictionary:
dict1 = { 'abc': 456 }
dict2 = { 'abc': 123, 98.6: 37 }
print(dict1,type(dict1))
print(dict2,type(dict2))
Output:
1.3 empty dictionary
s = {}
print(s,type(s))
Output:
1.4 factory function
d = dict()
print(d,type(d))
d = dict(a=1,b=2)
print(d,type(d))
Output:
1.5 nested dictionary
student = {
123:{
'name':'tom',
'age':18,
'score':99
},
456:{
'name':'lily',
'age':18,
'score':89
}
}
print(student)
print(student[123]['score'])
Output:
2. The dictionary features
Dictionary support member operators, support for loop, iterate dictionary.
Does not support indexing, slicing, and repeated the dictionary, connected meaningless, because only dictionary key value
2.1 member operator
Member operators is key for
d = {
'1':'a',
'2':'b'
}
print(d['1'])
print('1' in d)
print('a' in d)
Output:
2.2 for loop
The for loop is the key for
d = {
'1':'a',
'2':'b'
}
for key in d:
print(key)
Output:
2.3 traversing dictionary
d = {
'1':'a',
'2':'b'
}
for key in d:
print(key,d[key])
Output:
3. dictionary CRUD
Add 3.1 dictionary elements
Add an element
If there is a corresponding value of the key value is updated
if the key does not exist corresponding to the added value
service = {
'http':80,
'ftp':23,
'ssh':22
}
service['https'] = 443
print(service)
service['ftp'] = 21
print(service)
Output:
Increase the number of elements
service = {
'http':80,
'ftp':23,
'ssh':22
}
service_backup = {
'tomcat':8080,
'mysql':3306
}
# 增加多个元素,字典services中增加services_backup
service.update(service_backup)
print(service)
# 字典services中增加dns=53,flask=9000
service.update(dns=53,flask=9000)
print(service)
Output:
3.2 Delete dictionary elements
del delete
service = {
'http':80,
'ftp':23,
'ssh':22
}
del service['http']
print(service)
Output:
.pop delete specified key of key-value
if the key is present, delete the key and the value corresponding to the return value; if not, then the error
service = {
'http':80,
'ftp':23,
'ssh':22
}
item = service.pop('ssh')
print(item) #item为ssh的value值
print(service)
Output:
.popitem delete the last key-value value
service = {
'http':80,
'ftp':23,
'ssh':22
}
item = service.popitem()
print(item)
print(service)
Output:
Empty the contents of the dictionary: service.clear ()
service = {
'http':80,
'ftp':23,
'ssh':22
}
service.clear()
print(service)
Output:
3.3 modify the dictionary elements
.setdefault () adding key value
exists if the key value is not modified; if the key value does not exist, add the value of key-value
service = {
'http':80,
'ftp':23,
'ssh':22
}
service.setdefault('http',9090)
print(service)
service.setdefault('oracle',44575)
print(service)
Output:
3.4 dictionary elements View
View all of the dictionary key value
service = {
'http':80,
'ftp':23,
'ssh':22
}
print(service.keys())
Output:
View all value the value of the dictionary
service = {
'http':80,
'ftp':23,
'ssh':22
}
print(service.values())
Output:
Check the dictionary key-value pairs
service = {
'http':80,
'ftp':23,
'ssh':22
}
print(service.items())
Output:
View value of key values
.get method of obtaining the value corresponding to the value specified key values
if a key value exists, the value corresponding to the return value;
if the key value does not exist, default return None, if the need to specify the return value, the value can be passed
service = {
'http':80,
'ftp':23,
'ssh':22
}
print(service.get('https'))
print(service.get('https',443))
Output: