python-数据类型-字典和集合

>字典和集合

>>字典

>>>定义

  • 字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据

    • 变量后面接的是 {}
    • {} 内为空值时,则为空字典
    • 字典括号内为键值对格式 如

>>>特点

  键唯一,可哈希,不可修改(不可变类型)

    •   无序
  • 不可变类型有:整型,字符串,元组

  • 可变类型有:列表,字典

# 定义一个空字典
dic = {}
# 定义一个非空字典
dic1= {
    'name':'xiaohong',
    'age':18,
    'phone':13900001111
      }

注意:字典中的值不允许重复,如果重复了 新设置的内容会覆盖掉前面的内容

dic1= {
    'name':'xiaohong',
    'age':18,
    'phone':13900001111,
    'name':'honghua'
      }
print(dic1)
>>>dic1= {
    'name':'honghua',
    'age':18,
    'phone':13900001111
      }

>>>字典迭代输出

for循环实现
dic1= {
    'name':'xiaohong',
    'age':18,
    'phone':13900001111,
      }
for key in dic1:
    print(key,dic1[key])
>>>
name xiaohong
age 18
phone 13900001111
items()函数实现
dic1= {
    'name':'xiaohong',
    'age':18,
    'phone':13900001111,
      }
for key,value in dic1.items():
	print(key,value)
  >>>
name xiaohong
age 18
phone 13900001111

>>>字典操作函数

update字典数据更新
# 对已有的键 值数据进行更新 ,对没有的键 数据新增 
dic1 = {
    'name': 'xiaohong',
    'age': 18,
    'phone': 13900001111
}
dic1.update( {
    'name': 'xiaoli',
    'age': 21,
    'phone': 13903301111,
    'adress':'xxxxxx'
})
print(dic1)
>>>{'name': 'xiaoli', 'age': 21, 'phone': 13903301111,'adress':'xxxxxx'}
del 字典数据的删除
dic1 = {
    'name': 'xiaohong',
    'age': 18,
    'phone': 13900001111
}

del dic1['phone']
print(dic1)
>>>
 {
    'name': 'xiaohong',
    'age': 18
 }
pop字典数据弹出
dic1 = {
    'name': 'xiaohong',
    'age': 18,
    'phone': 13900001111
}
dic2={
    'name': 'xiaohong',
    'age': 18,
    'phone': 13900001111}
dic1.pop('name')     # 使用pop弹出指定的键
print(dic1)
dic2.popitem() # 使用popitem弹出最后一位数据 
print(dic2)
>>>
{'age': 18, 'phone': 13900001111}
{'name': 'xiaohong', 'age': 18}
formkeys()将序列转为字典
# 设置一个元组 将元组的数据作为key 并将设置的内容相同
dict_1= dict.fromkeys(('name','age'),'xiaohong')
print(dict_1)
>>>{'name': 'xiaohong', 'age': 'xiaohong'}
# 设置一个字符串 会自动取每一个字符作为key 并设置相同的内容
dict_1= dict.fromkeys('abcdefg','age')
print(dict_1)
>>>{'a': 'age', 'b': 'age', 'c': 'age', 'd': 'age', 'e': 'age', 'f': 'age', 'g': 'age'}
get 函数查询
# 语法 dict.get(key)
dic1 = {
    'name': 'xiaohong',
    'age': 18,
    'phone': 13900001111
}
print(dic1.get('name'))
>>>xiaohong
keys() 取出字典中所有的键
dic1 = {
    'name': 'xiaohong',
    'age': 18,
    'phone': 13900001111
}
keys= dic1.keys()
print(keys)
>>>dict_keys(['name', 'age', 'phone'])
values()取值字典的所有值
dic1 = {
    'name': 'xiaohong',
    'age': 18,
    'phone': 13900001111
}
values= dic1.values()
print(values)
>>>dict_values(['xiaohong', 18, 13900001111])

>>>字典推导式

dic1 = {1:i+1 for i in range(10)} # 推导式就是将普通的for循环压缩成了一行代码,这种压缩语法适用于列表,字典,集合等可迭代的数据结构

>>集合

>>>定义

可变集合(set)

    • 变量后面接的是 {},但是括号内不是键值对格式
# 定义一个空集合
se = set()  
# 定义一个非空集合
set1 = {1, 2, 3, 4, 5}

>>>特点

    • 自动去重
    • 无序的
    • 集合 ,集合只有值,没有键,集合最大的特性就是里面的数据是不会重复的,所以常见的应用对列表去重
    • 集合是可变的是可以修改的

去重

lis = [1, 1, 2, 3, 4, 3, 4, 5]
lis2 = set(lis)
print(lis2)
>>>{1, 2, 3, 4, 5}

>>>集合操作函数

add添加元素
se = {1, 2, 3, 4, 5}
se.add('xxxx')
print(se)
>>> {'xxxx', 1, 2, 3, 4, 5}
remove移除元素
se = {1, 2, 3, 4, 5}
se.remove(1)  # 根据详细数据移除
print(se)
>>>{2, 3, 4, 5}
update() 更新元素
# 更新数据 等同于列表的extend
se = {1, 2, 3, 4, 5}
se.update({111, 222, 333, 444}) 
print(se)
>>>{1, 2, 3, 4, 5, 333, 111, 444, 222}
copy()复制数据
se = {1, 2, 3, 4, 5}
se1=se.copy()  # 复制
print(se,'\n',se1)
>>>{1, 2, 3, 4, 5} 
 {1, 2, 3, 4, 5}
clear()清空数据
se = {1, 2, 3, 4, 5}
se.clear()  # 清空
print(se)
>>>set() # 表示空集合
posted @ 2022-03-15 01:33  饭兜  阅读(122)  评论(0编辑  收藏  举报