Learn python dictionary (dictionary)

1. What is a dictionary object

  • Dictionary is another variable container model, and can store any type of object.
  • Each key-value key=>value pair of the dictionary is separated by a colon:, and each key-value pair is separated by a comma. The entire dictionary is enclosed in curly braces {}, the format is as follows:
  • The key types must be the same, such as string, int, etc.
  • The value type can be non-unique

2. Create a dictionary object

# coding=utf-8

## 字典类型的创建
## 使用花括号来创建
dict01={
    
    'name':'张三','age':18,'score':98.5}
print(dict01)

## 通过函数dict来创建
## dict使用list,元组的方式来创建
dict02=dict([('name','张三'),('age',18),('score',99.8)])
print(dict02)

## 通过fromkey函数来创建只有key的dict对象
dict03=dict.fromkeys(['name','age','score'])
print(dict03)

result

{
    
    'name': '张三', 'age': 18, 'score': 98.5}
{
    
    'name': '张三', 'age': 18, 'score': 99.8}
{
    
    'name': None, 'age': None, 'score': None}

3. Get the content in the dict

3.1 By way of [key]

dictObject={
    
    'name':'张三','age':19,'score':99.6}
print(dictObject['name'])	# 结果为张三
print(dictObject['sex'])	# 没有sex这个键,抛出异常

Get the value in this way, if the current key does not exist, directly report an exception

3.2 Through the get() function (recommended)

Use the get function to get, if the current key does not exist, only the None value will be returned, no exception will be thrown, or we can set the default value to return

## 通过get()函数获取
dictObject={
    
    'name':'张三','age':19,'score':99.6}
print(dictObject.get('name'))	# 结果为“张三”
# 设置默认值,如果没有则返回默认值
print(dictObject.get('sex','null'))

4. Add, modify, delete elements

4.1 Add

If the added element has the same key name as an element in the dictionary, the previous key-value pair will be overwritten

>>> a={
    
    "name":"张三",'age':18,'score':99.9}
>>> a['sex']='男'
>>> a
{
    
    'name': '张三', 'age': 18, 'score': 99.9, 'sex': '男'}

4.2 Modification

Just overwrite the old elements

>>> a={
    
    "name":"张三",'age':18,'score':99.9}
>>> a['name']='李四'
>>> a
{
    
    'name': '李四', 'age': 18, 'score': 99.9}
>>> 

4.3 update

The dictionary object provides an update method that can accept a dictionary type to update the current dictionary object

The same elements are directly covered, and new elements are directly added

>>> b={
    
    'name':'张三','sex':'男'}
>>> a={
    
    'score':99,'name':'李四'}
>>> a.update(b)
>>> a
{
    
    'score': 99, 'name': '张三', 'sex': '男'}
>>> 

4.4 Delete elements

Delete specified element

The pop function deletes the key-value pair represented by the specified key and returns the content of the value

>>> a={
    
    'name':'张三','age':18,'sex':'男'}
>>> a
{
    
    'name': '张三', 'age': 18, 'sex': '男'}
>>> a.pop('name')
'张三'
>>> a
{
    
    'age': 18, 'sex': '男'}

Delete all elements

The clear function will delete all objects in the dictionary


>>> a={
    
    "name":"张三",'age':18,'score':99.9}
>>> a
{
    
    'name': '张三', 'age': 18, 'score': 99.9}
>>> a.clear()
>>> a
{
    
    }
>>> 

popitem() function

Randomly delete a key-value pair in a dictionary, and return the key-value pair.
 
Because a dictionary is different from a collection, a dictionary is unordered and has no first element. It is used when deleting dictionary elements out of order

>>> a={
    
    "name":"张三",'age':18,'score':99.9}
>>> a
{
    
    'name': '张三', 'age': 18, 'score': 99.9}
>>> a.popitem()
('score', 99.9)
>>> a
{
    
    'name': '张三', 'age': 18}
>>> a.popitem()
('age', 18)
>>> a
{
    
    'name': '张三'}
>>> 

5. Sequence unpacking

dict01={
    
    
    'name':'Daiv',
    'age':18,
    'sex':'男',
    'score':99.8
}

# 默认以解包的方式获取所有的键
a,b,c,d=dict01
print(a,end='\t')
print(b,end='\t')
print(c,end='\t')
print(d,end='\t')
print()

# 获取键
a,b,c,d=dict01.keys()
print(a,end='\t')
print(b,end='\t')
print(c,end='\t')
print(d,end='\t')
print()

# 获取值
a,b,c,d=dict01.values()
print(a,end='\t')
print(b,end='\t')
print(c,end='\t')
print(d,end='\t')
print()

# 获取键值对对象
a,b,c,d=dict01.items()
print(a,end='\t')
print(b,end='\t')
print(c,end='\t')
print(d,end='\t')
print()

result

name    age     sex     score   
name    age     sex     score
Daiv    1899.8
('name', 'Daiv')        ('age', 18)     ('sex', '男')   ('score', 99.8)

Guess you like

Origin blog.csdn.net/qq_42418169/article/details/109557243