Python dictionary entry basis 4-1

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

  1. Dictionary use braces {}or dictto create, dictionaries can be nested to use
  2. Dictionary come in pairs, Dictionary embodied in the form of key (key) value (value)
  3. Colon between the key and values :separated by a comma between each key-value pairs ,separated
  4. The dictionary keyis the only, and valuecan be repeated
  5. The dictionary keydoes not use Chinese or other characters, which is the industry common practices

Create dictionary

Dictionary can use curly braces {}or dictfunction 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 dictfunction to create a dictionary

dictPython is a built-in function, the function is used when creating the list, key unquoted
dicteffect 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. fromkeysMethods

fromkeysMethods and dictfunctions 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 fromkeysreturn a default value specified method, by default None
if a change in this case, only the fromkeysmethod 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/Ais 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 KeyErroran error

emp = {'name':'张三' , 'age':22 , 'sex':'男'}
print(emp['name'])
# 运行结果:张三
2. Second way

Use dictionary getmethod values

If the key does not exist, return Noneor 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)
# 运行结果:{}

Guess you like

Origin www.cnblogs.com/dazhi-blog/p/11520742.html