python study notes (two)-hash type (dictionary, set)

table of Contents

set

Unordered (the element position is not fixed), unique (each element is unique), and variable type (the element is variable).

set Function method Annotation
increase add Add elements to the collection.
delete pop Remove and return any set element.
remove Remove an element from the collection; it must be a member.
change update Update the collection with the union of itself and other elements

1. Definition:

#方法一 直接赋值
>>> set1 = {
    
    1,2,3,4}
>>> type(set1)
<class 'set'>
>>> set1
{
    
    1, 2, 3, 4}

#方法二 s=set()赋值set()
>>> set2 = set()
>>> type(set2)
<class 'set'>

2. Increase

#方法一  .add() 每次只能添加一个元素
>>> set2.add(1)
>>> set2
{
    
    1}

#方法二 .update([元素1,元素2···])  可以同时添加多个元素
>>> set2.update([2,3,4])
>>> set2
{
    
    1, 2, 3, 4}

3. Delete

#方法一 .pop() 随机删除
>>> set1
{
    
    1, 2, 3, 4, 5}
>>> set1.pop()
1 
>>> set1
{
    
    2, 3, 4, 5}

#方法二 .remove(元素) 指定元素删除
>>> set1
{
    
    2, 3, 4, 5}
>>> set1.remove(5)
>>> set1
{
    
    2, 3, 4}

#方法三 .clear()清空集合
>>> set1
{
    
    2, 3, 4}
>>> set1.clear()
>>> set1
set()

4. Change: The set cannot modify the specified element, you can use remove to delete the specified element first, and then add
5. Set operation

Operator operation python operator
Intersection &
Union
Subtraction -
set1 = {
    
    1,2.5,True,complex(2,3)}
set2 = {
    
    3,3.4,True,complex(1,3)}
print('集合1:',set1)
print('集合2:',set2)
print('交集:',set1&set2)
print('并集:',set1|set2)
print('差集set1-set2:',set1-set2)
print('差集set2-set1:',set2-set1)

结果:
集合1: {
    
    1, 2.5, (2+3j)}
集合2: {
    
    3.4, True, (1+3j), 3}
交集: {
    
    1}
并集: {
    
    1, 2.5, 3.4, 3, (1+3j), (2+3j)}
差集set1-set2: {
    
    2.5, (2+3j)}
差集set2-set1: {
    
    3, (1+3j), 3.4}

Insert picture description here

dictionary

Note: The key value of the dictionary is unique (cannot be repeated), but the value may not be unique

dict Function method Annotation
increase setdefault Check if you have it, increase if you don't
change update Change if you have it, increase if you don't
delete pop Find out the key-value pair of the specified key
I drink Return and delete the last pair of keys and values ​​in the dictionary
check get Get the value corresponding to the specified key, if the key does not exist, return it to none
keys Remove all keys
values Get all the values
items Take out all the key-value pairs

1. Definition:

#方法一 di = {key:value}键值对形式
>>> dict={
    
    'name':'zhangsan','age':'18'}
>>> dict
{
    
    'name': 'zhangsan', 'age': '18'}

#方法二 di = {}   直接赋值创建
>>> dict2={
    
    }
>>> type(dict2)
<class 'dict'>

2. Increase

#方法一  di[key] = value  di是自己定义的变量名,没有对应的key时为增,有的时候是改
>>> dict2['name']='lisi'
>>> dict2
{
    
    'name': 'lisi'}

#方法二 .setdefault(key,value)  有则查,无则增
>>> dict2
{
    
    'name': 'lisi'}
>>> dict2.setdefault('name','lisi')  #name存在,查找name对应值
'lisi'
>>> dict2.setdefault('age','14') #age不存在,添加
'14'
>>> dict2
{
    
    'name': 'lisi', 'age': '14'}

3. Delete

#方法一  .pop(key)  删除指定的值
>>> dict2
{
    
    'name': 'lisi', 'age': '14'}
>>> dict2.pop('age')
'14'
>>> dict2
{
    
    'name': 'lisi'}

#方法二 .popitem() 删除字典最后一对键值对
>>> dict2
{
    
    'name': 'lisi', 'age': '14', 'from': 'hujian'}
>>> dict2.popitem()
('from', 'hujian')
>>> dict2
{
    
    'name': 'lisi', 'age': '14'}

#方法三  .clear() 清空字典
>>> dict2
{
    
    'name': 'lisi', 'age': '14'}
>>> dict2.clear()
>>> dict2
{
    
    }

4. Change

#方法一 .update ({key:value})  有则改,无则添加
>>> dict
{
    
    'name': 'zhangsan', 'age': '20'}
>>> dict.update({
    
    'age':'18'})  #age存在,修改age的value
>>> dict
{
    
    'name': 'zhangsan', 'age': '18'}
>>> dict.update({
    
    'high':'178'})  #high不存在,添加high
>>> dict
{
    
    'name': 'zhangsan', 'age': '18', 'high': '178'}

5. Check

#方法一 .[key]  查找显示所有的key值
>>> dict
{
    
    'name': 'zhangsan', 'age': '18', 'high': '178'}
>>> dict.keys()
dict_keys(['name', 'age', 'high'])

#方法二 .get(key) 获取指定key对应的value
>>> dict
{
    
    'name': 'zhangsan', 'age': '18', 'high': '178'}
>>> dict.get('name')
'zhangsan'

#方法三 .values()  查找显示所有的value值
>>> dict
{
    
    'name': 'zhangsan', 'age': '18', 'high': '178'}
>>> dict.values()
dict_values(['zhangsan', '18', '178'])

#方法四 .items()  显示所有键值对
>>> dict
{
    
    'name': 'zhangsan', 'age': '18', 'high': '178'}
>>> dict.items()
dict_items([('name', 'zhangsan'), ('age', '18'), ('high', '178')])

Guess you like

Origin blog.csdn.net/qq_46485161/article/details/115146064