Python dictionary common functions {clear(), copy(), fromkeys(), get(), items(), keys(), setdefault(), update(), values()}

1. General introduction

function introduce
clear() delete all items in the dictionary
copy() make a copy of the dictionary
fromkeys() Create and return a new dictionary, or the elements in the iterable object can be used as keys in the dictionary
get()

Returns the value for the specified key

items() Returns a iterable array of (key/value) tuples as a list
keys() Return all keys of a dictionary as a list
setdefault() Similar to the get() function, returns the value of the key; if the key does not exist, add the key and set the corresponding value to the default value
update() Used to update the key-value pairs in the dictionary, you can also modify the value or add new key-value pairs
values() Return all values ​​of the dictionary as a list

2. Explanation and examples of each function

1. clear() function

clear() removes all items in the dictionary

Syntax: dictname.clear()

#创建一个字典,姓名为翠花,年龄18岁,性别女
dict={'name':'翠花','age':18,'sex':'女'}
dict.clear()#清除字典内所有元素,返回空字典
print(dict)
#{}

Two, copy () function

copy() copies the dictionary

Syntax: dictname.copy()

#创建一个字典,姓名为翠花,年龄18岁,性别女
dict={'name':'翠花','age':18,'sex':'女'}
dict1=dict.copy()#对原字典进行复制,复制后的字典为dict1
print(dict1)
#{'name': '翠花', 'age': 18, 'sex': '女'}

#接下来,对dict1进行修改,观察两个字典变化
dict1['name']='小王'
print(dict)#{'name': '翠花', 'age': 18, 'sex': '女'}
print(dict1)#{'name': '小王', 'age': 18, 'sex': '女'}
#可见,对复制后的字典进行修改,不会改变原字典

Three, fromkeys () function

fromkeys() creates and returns a new dictionary, or iterates over the elements in the object as keys in the dictionary

Syntax: dictname.fromkeys(key,value), where key can be a string, list, or tuple; value defaults to None

list=[1,2,3,4,5]
dict1=dict.fromkeys(list,10)#将value设置为10
print(dict1)
#{1: 10, 2: 10, 3: 10, 4: 10, 5: 10}

Four, get () function

get() returns the value for the specified key

Syntax: dictname.get(key), when the value corresponding to the key of the index does not exist, return None

dict1={'name':'翠花','age':18,'sex':'女'}
dict2=dict1.get('name')
print(dict2)
#翠花

dict2=dict1.get('address')
print(dict2)
#None

5. items() function

items() returns a iterable array of (key/value) tuples as a list

Syntax: dictname.items() takes no arguments

dict1={'name':'翠花','age':18,'sex':'女'}
dict2=dict1.items()
print(dict2)
#dict_items([('name', '翠花'), ('age', 18), ('sex', '女')])

Six, keys () function

keys() returns all keys of a dictionary as a list

Syntax: dictname.keys() takes no arguments

dict1={'name':'翠花','age':18,'sex':'女'}
dict2=dict1.keys()
print(dict2)
#dict_keys(['name', 'age', 'sex'])

Seven, setdefault () function

setdefault() is similar to the get() function, returning the value of the key; if the key does not exist, add the key and set the corresponding value to the default value

Syntax: dictname.setdefault(key,default=None)

dict1={'name':'翠花','age':18,'sex':'女'}
dict2=dict1.setdefault('sex')
print(dict2)
#女
dict2=dict1.setdefault('address')
print(dict2)#None
print(dict1)#{'name': '翠花', 'age': 18, 'sex': '女', 'address': None}
#可以看见,添加了新的键值对

Eight, update () function

update() is used to update the key-value pairs in the dictionary, and can also modify the value or add new key-value pairs (the same key will be overwritten)

Syntax: dictname.update (dictionary or sequence of key-value pairs)

#示例一:
dict2={1:2,2:3,3:4}
dict2.update({1:3,2:6,3:9})#更改键对应的值
print(dict2)
#{1: 3, 2: 6, 3: 9}
#示例二:
dict2.update({4:3,5:6,6:9})#添加键值对
print(dict2)
#{1: 3, 2: 6, 3: 9, 4: 3, 5: 6, 6: 9}
#示例三:
dict2.update([(7,10),(8,10)])#传入元组组成的列表
print(dict2)
#{1: 3, 2: 6, 3: 9, 4: 3, 5: 6, 6: 9, 7: 10, 8: 10}

Nine, values ​​() function

values() returns all values ​​of the dictionary as a list

Syntax: dictname.values() takes no arguments

dict2={1:2,2:3,3:4}
print(dict2.values())
#dict_values([2, 3, 4])

Guess you like

Origin blog.csdn.net/weixin_57501965/article/details/126186166