introduction
Python dictionary is built in a data structure, which facilitates the expression of a number of semantic data structure, commonly used in the development of the dictionary is a data structure
Introduction dictionary
- Dictionary use braces
{}
ordict
to create, dictionaries can be nested to use - Dictionary come in pairs, Dictionary embodied in the form of key (key) value (value)
- Colon between the key and values
:
separated by a comma between each key-value pairs,
separated - The dictionary
key
is the only, andvalue
can be repeated - The dictionary
key
does not use Chinese or other characters, which is the industry common practices
Create dictionary
Dictionary can use curly braces
{}
ordict
function to create
1. Use curly braces {}
to create a dictionary
Note: use a comma between each key-value pair dictionaries ,
are separated
emp = {'name':'张三' , 'age':22 , 'sex':'男'}
print(emp)
# 运行结果:{'name': '张三','age': 22, 'sex': '男'}
# 字典打印时会将花括号也打印出来
print(type(emp))
# 运行结果:<class 'dict'>
# 从以上打印类型可以看出变量属于 dict 字典类型
2-1 Using dict
function to create a dictionary
dict
Python is a built-in function, the function is used when creating the list, key unquoted
dict
effect functions and operating results of the above example is the same
emp = dict(name='张三' , age=22,sex='男')
print(emp)
# 运行结果:{'name': '张三', 'age': 22, 'sex': '男'}
2-2. fromkeys
Methods
fromkeys
Methods anddict
functions with the use, it allows the use of sequence to create a dictionary key
emp = dict.fromkeys(['name' , 'age' , 'sex'])
print(emp)
# 运行结果:{'name': None, 'age': None, 'sex': None}
If not fromkeys
return a default value specified method, by default None
if a change in this case, only the fromkeys
method of the second parameters into the default value, the following example
emp = dict.fromkeys(['name' , 'age' , 'sex'] , 'N/A')
print(emp)
# 运行结果:{'name': 'N/A', 'age': 'N/A', 'sex': 'N/A'}
The default value N/A
is not necessary, the corresponding values can be passed in accordance with the actual needs of the project, with or without incoming values
A list of values
Dictionary value in two ways
1. a way
Use square brackets dictionary passed behind the dictionary variables were key value
This approach has a downside: If the key does not exist in the dictionary, it will report KeyError
an error
emp = {'name':'张三' , 'age':22 , 'sex':'男'}
print(emp['name'])
# 运行结果:张三
2. Second way
Use dictionary
get
method values
If the key does not exist, return None
or their specified value, for example N/A
, the following sample code
emp = {'name':'张三' , 'age':22 , 'sex':'男'}
v = emp.get('name')
print(v)
# 运行结果:张三
emp = {'name':'张三' , 'age':22 , 'sex':'男'}
v = emp.get('dept','其他部门')
print(v)
# 运行结果:其他部门
Dictionary writes
Dictionary CRUD operations
Update
Python dictionary uphold: "There are updates, if any, new" principle
1. Update the Dictionary Value
emp = {'name':'张三' , 'age':22 , 'sex':'男' , 'dept':'研发部'}
emp['dept'] = '推广部'
print(emp)
# 运行结果:{'name': '张三', 'age': 22, 'sex': '男', 'dept': '推广部'}
2. Batch Update
Using the
update()
method of batch update the dictionary, corresponding to the incoming in the corresponding method key, value
emp = {'name':'张三' , 'age':22 , 'sex':'男' , 'dept':'研发部'}
emp.update(age=18 , dept='推广部')
print(emp)
# 运行结果:{'name': '张三', 'age': 18, 'sex': '男', 'dept': '推广部'}
New
Add a list of operations and update operations are substantially the same, Python adhering dictionary: "There is updated, the new free" principle
, when there is a corresponding key to perform the update the dictionary, the dictionary performed when the corresponding new key does not exist
1. New operation
emp = {'name': '张三', 'age': 22, 'sex': '男', 'dept': '研发部'}
emp['job'] = '销售'
print(emp)
# 运行结果:{'name': '张三', 'age': 22, 'sex': '男', 'dept': '研发部', 'job': '销售'}
2. bulk operations
emp = {'name': '张三', 'age': 22, 'sex': '男', 'dept': '研发部'}
emp.update(dept='推广部' , job='推广员')
print(emp)
# 运行结果:{'name': '张三', 'age': 22, 'sex': '男', 'dept': '推广部', 'job': '推广员'}
delete
1. Using the pop()
method to delete
The method removes the dictionary corresponding to a given key value and
pop()
returns a value, the return value is deleted
emp = {'name': '张三', 'age': 22, 'sex': '男', 'dept': '研发部'}
dept = emp.pop('dept')
print(emp)
# 运行结果:{'name': '张三', 'age': 22, 'sex': '男'}
print(dept)
# 运行结果:研发部
2. popitems()
Delete the last kv dictionary
popitems()
It returns a tuple
emp = {'name': '张三', 'age': 22, 'sex': '男', 'dept': '研发部'}
kv = emp.popitem()
print(emp)
# {'name': '张三', 'age': 22, 'sex': '男'}
print(kv)
# 运行结果:('dept', '研发部')
3. Clear List clear()
emp = {'name': '张三', 'age': 22, 'sex': '男', 'dept': '研发部'}
emp.clear()
print(emp)
# 运行结果:{}