1、dict,字典
字典是以键值对(key-value)的形式存储数据的一种数据类型,在字典中键必须是唯一的,且是不可变的数据类型(int,str,tuple,bool)
通过键来找到对应的值
Ⅰ、增
(1)、直接赋值
有则修改,无则添加
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
dic['age'] = 20
dic['school'] = 'lalala'
print(dic)
{'name': 'abc', 'age': 20, 'hobby': 'sleep', 'school': 'lalala'}
(2)、setdefault()
有则不变,无则添加
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
dic.setdefault('age',20)
dic.setdefault('school','lalala')
print(dic)
{'name': 'abc', 'age': 18, 'hobby': 'sleep', 'school': 'lalala'}
Ⅱ、删
(1)pop
根据键弹出一个值,参数如下:
pop(key,default) key:要弹出的值的键 default:如果此键不在字典里,返回的值,如果不写默认报错
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
a = dic.pop('name')
print(a,dic)
abc {'age': 18, 'hobby': 'sleep'}
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
a = dic.pop('school')
print(a,dic)
KeyError: 'school'
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
a = dic.pop('school','此键不在字典中!')
print(a)
print(dic)
此键不在字典中!
{'name': 'abc', 'age': 18, 'hobby': 'sleep'}
(2)、popitem()
随机弹出一对键值对,返回形式为元组形式的键值对,例如:
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
a = dic.popitem()
print(a)
print(dic)
('hobby', 'sleep')
{'name': 'abc', 'age': 18}
(3)、clear
清空字典
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
dic.clear()
print(dic)
{}
(4)、del
将字典在内存中删除
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
del dic
print(dic)
NameError: name 'dic' is not defined
Ⅲ、改
(1)、直接赋值
见 增 的 直接赋值
(2)、update()
用一个其他字典更新原字典
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
dic2 = {'school':'lalala','date':'aaaa'}
dic.update(dic2)
print(dic)
{'name': 'abc', 'age': 18, 'hobby': 'sleep', 'school': 'lalala', 'date': 'aaaa'}
如果其他字典中有与原字典相同的键,则覆盖原字典中的值
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
dic2 = {'name':'AAA','school':'lalala'}
dic.update(dic2)
print(dic)
{'name': 'AAA', 'age': 18, 'hobby': 'sleep', 'school': 'lalala'}
Ⅳ、查
(1)、以键查值
有一个缺点,找不到会报错
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
print(dic['name'])
abc
(2)、get()
可自定义找不到返回的内容
参数 get(键,找不到返回的内容) 找不到返回的内容默认为None
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
print(dic.get('name'))
print(dic.get('school'))
print(dic.get('school','没有此键'))
abc
None
没有此键
(3)、keys()
找到字典中所有的键,返回的数据类型为 dict_keys , 可与for搭配遍历字典的值
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
print(dic.keys())
dict_keys(['name', 'age', 'hobby'])
(4)、values()
找到字典中所有的值,返回的数据类型为 dict_values , 可与for搭配遍历字典的值
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
print(dic.values())
dict_values(['abc', 18, 'sleep'])
(5)、items()
找到字典中所有的j键值对,返回的数据类型为 dict_items ,
可与for搭配遍历字典的键值对,循环时可以用两个变量分别接收键和值
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
print(dic.items())
dict_items([('name', 'abc'), ('age', 18), ('hobby', 'sleep')])
dic = {'name':'abc', 'age':18, 'hobby':'sleep'}
for i,j in dic.items() :
print(i,j)
name abc
age 18
hobby sleep
2、set,集合
集合内的元素无序,不重复,可哈希,但集合本身是不可哈希的(不能作为字典的key)
集合主要有两大作用:
1、去重
2、数据关系测试
一、操作
Ⅰ、增
(1)、add()
set1 = {'abc', 18 ,'sleep'}
set1.add('lalala')
print(set1)
{'abc', 18, 'sleep', 'lalala'}
(2)、update()
迭代添加,添加的内容必须是可迭代的
set1 = {'abc', 18 ,'sleep'}
set1.update('def')
print(set1)
{'d', 'sleep', 'e', 18, 'f', 'abc'}
Ⅱ、删
(1)、remove()
remove的元素必须是集合内有的
set1 = {'abc', 18 ,'sleep'}
set1.remove(18)
print(set1)
{'abc', 'sleep'}
(2)、pop()
随机弹出一个元素
set1 = {'abc', 18 ,'sleep'}
a = set1.pop()
print(a,set1)
sleep {18, 'abc'}
(3)、clear,del
与其他数据类型用法相同
Ⅲ、查
由于集合的无序性,所以必须用for循环来遍历集合
二、作用
Ⅰ、去重
list1 = [111,222,333,111,222,333,555,444]
set1 = set(list1)
print(set1)
{555, 333, 111, 444, 222}
可以看到,集合和字典一样是用{}定义的,所以在定义一个空集合时,不能用{},而用set()
a = {}
b = set()
print(type(a))
print(type(b))
<class 'dict'>
<class 'set'>
Ⅱ、数据关系测试
(1)、交集 &或intersection
set1 = {1,2,3,4,5}
set2 = {3,4,5,6,7}
a = set1 & set2
print(a)
{3, 4, 5}
(2)、并集 | 或 union
set1 = {1,2,3,4,5}
set2 = {3,4,5,6,7}
a = set1 | set2
print(a)
{1, 2, 3, 4, 5, 6, 7}
(3)、判断是否时 子集(issubset)或 超集(issuperset)
set1 = {3,4,5}
set2 = {3,4,5,6,7}
print(set1.issubset(set2))
print(set2.issuperset(set1))
True
True
(4)、差集
直接进行减运算
set1 = {1,2,3,4,5}
set2 = {3,4,5,6,7}
print(set1 - set2)
print(set2 - set1)
{1, 2}
{6, 7}
(5)、反交集 ^ 或 symmetric_difference
set1 = {1,2,3,4,5}
set2 = {3,4,5,6,7}
print(set1 ^ set2)
{1, 2, 6, 7}