09.Python开发基础(数据类型之集合)


一、集合的特性

01.集合里面的元素(数据)得是不可变数据类型,例如:数字、字符串类型;
02.集合天生去重,里面的元素是无序的,所以集合中的元素也没有相应的下标;
03.集合因为是无序的,所以{"3,","4","5"}和{"5","3","4"}是同一个集合;

二、集合的创建

>>> a = {1,2,3,4,2,"chenliang","2","xiaxia","chenliang"} # 若a={},则类型是dict类型
>>>
>>> type(a)

>>> 
>>> a	# 从结果可以看出有两个2(集合不是去重嘛?),这是因为它们的数据类型是不一样的哈
{'xiaxia', 1, 2, 3, 4, 'chenliang', '2'}

三、集合的增加(增加的数据只能是不可变的数据类型)

## 定义源数据
>>> set1 = {1,2,3,4,2,"chenliang","lili","yinyin","lili"}
>>> type(set1)

>>> set1
{1, 2, 3, 4, 'chenliang', 'yinyin', 'lili'}
>>>

## 往集合set1中添加数据(字符数据类型,成功)
>>> set1.add("binbin")
>>> set1
{1, 2, 3, 4, 'chenliang', 'yinyin', 'binbin', 'lili'}

>>> set1.add("binbin")
>>> set1
{1, 2, 3, 4, 'chenliang', 'yinyin', 'binbin', 'lili'}

PS:添加了两次"binbin",其结果只有一个"binbin",这是因为集合天生去重


## 往集合set1中添加数据(数字类型,成功)
>>> set1.add(9)
>>> set1
{1, 2, 3, 4, 'yinyin', 9, 'binbin', 'lili', 'chenliang'}

>>> set1.add(9)
>>> set1
{1, 2, 3, 4, 'yinyin', 9, 'binbin', 'lili', 'chenliang'}

PS:添加了两次9,其结果只有一个9,这是因为集合天生去重


## 往集合set1中添加数据(元组类型,成功)
>>> set1.add((1,2,3))
>>> set1
{1, 2, 3, 4, 'yinyin', 9, 'binbin', 'lili', (1, 2, 3), 'chenliang'}

>>> set1.add((1,2,3))
>>> set1
{1, 2, 3, 4, 'yinyin', 9, 'binbin', 'lili', (1, 2, 3), 'chenliang'}

PS:添加了两次元组(1,2,3),其结果只有一个(1,2,3),这是因为集合天生去重


## 往集合set1中添加数据(列表类型,失败)
>>> set1.add([1,2,3])
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type: 'list'

四、集合的查询(注意集合中元素的数据类型哈)

## 定义源数据
>>> set2 = {1, 2, 3, 4, 'yinyin', 9, 'binbin', 'lili', (1, 2, 3), 'chenliang'}
>>>
>>> type(set2)

>>>
>>> set2
{1, 2, 3, 4, 'yinyin', 9, 'binbin', 'lili', (1, 2, 3), 'chenliang'}


## 查询某个元素是否在集合中(注意元素的数据类型哈)
>>> "1" in set2
False
>>>
>>> 1 in set2
True 

五、集合的修改

不好意思,集合中的元素不能修改

六、集合的删除

## 删除的相关方法总结
集合名.pop()            # 随机删除一个元素
集合名.discard("元素")  # 删除集合中的元素,若元素不存在,不会报错
集合名.remove("元素")   # 删除集合中的元素,若元素不存在,则会报错


## 准备源数据
>>> set3 = {1, 2, 3, 4, 'yinyin', 9, 'binbin', 'lili', (1, 2, 3), 'chenliang'}
>>> type(set3)

>>> set3
{1, 2, 3, 4, 'yinyin', 9, 'binbin', 'lili', (1, 2, 3), 'chenliang'}


# 集合名.pop() 方法示例
>>> set3.pop()                 # 从返回值看,删除的是元素1,这里是在交互模式下
1
>>> set3                       # 随机删除一个元素后的集合set3
{2, 3, 4, 'yinyin', 9, 'binbin', 'lili', (1, 2, 3), 'chenliang'}


# 集合名.discard("元素") 方法示例
>>> set3.discard("陈亮")       # 删除一个不存在的元素"陈亮",没有报错
>>> set3
{2, 3, 4, 'yinyin', 9, 'binbin', 'lili', (1, 2, 3), 'chenliang'}
>>>
>>> set3.discard("chenliang")  # 删除元素"chenliang",这个元素是存在的
>>> set3
{2, 3, 4, 'yinyin', 9, 'binbin', 'lili', (1, 2, 3)}


# 集合名.remove("元素") 方法示例
>>> set3.remove("lili")        # 删除元素"lili",这个元素是存在的
>>> set3
{2, 3, 4, 'yinyin', 9, 'binbin', (1, 2, 3)}
>>>
>>> set3.remove("陈亮")        # 删除一个不存的元素"陈亮",直接报错
Traceback (most recent call last):
  File "", line 1, in 
KeyError: '陈亮'

七、集合的关系运算

## 交集(&)运算(把两个集合中相同的元素(数据类型也要一样)拿出来)
>>>
>>> set4 = {1,2,"3"}
>>> set5 = {1,2,3}
>>>
>>> set4 & set5
{1, 2}
>>> set5 & set4
{1, 2}
>>>

## 并集(|)运算(所有元素合在一起,且把相同的元素(数据类型也一样的)去重)
>>>
>>> set4 = {1,2,"3"}
>>> set5 = {1,2,3}
>>>
>>> set4 | set5
{1, '3', 2, 3}
>>> set5 | set4
{1, 2, 3, '3'}
>>>

## 差集(-)运算
>>> set4 = {1,2,"3"}
>>> set5 = {1,2,3}
>>>
>>> set4 - set5   #以set4集合为基础(拿set5和set4作比较,set5较set4差一个"3")
{'3'}
>>> set5 - set4   #以set5集合为基础(拿set4和set5作比较,set4较set5差一个3)
{3}
>>>

## 对称差集(^)运算
>>>
>>> set4 = {1,2,"3"}
>>> set5 = {1,2,3}
>>>
>>> set4 ^ set5  #互相作为基础(set5和set4作比较,相差一个"3";set4和set5相比较,相关一个3;)
{'3', 3}
>>> set5 ^ set4  #互相作为基础(set5和set4作比较,相差一个"3";set4和set5相比较,相关一个3;)
{'3', 3}
>>>

八、集合间的关系判断

 五、集合的关系的判断

两个集合之间一般有三种关系:相交、包含、不相交。在Python中分别用下面的方法来判断
>>> {1,2,3}.issubset({1,2,3})      #判断是不是相交,明显相交,
True
>>> {1,2,3}.issubset({1,2,3,4})    #判断是不是相交,明显示有元素相交的嘛
True

>>> {1,2,3}.issubset({1,2,3})      #判断是不是子集
True
>>> {1,2,3}.issubset({1,2,3,4})    #判断是不是子集
True

>>> {1,2,3}.issuperset({1,2,3})    #判断{1,2,3}是不是{1,2,3}的父集
True
>>> {1,2,3}.issuperset({1,2,3,4})  #判断{1,2,3}是不是{1,2,3,4}的父集
False
>>> {1,2,3,4}.issuperset({1,2,3})  #判断{1,2,3,4}是不是{1,2,3}的父集
True