集合是一种无序可变的容器,对应数学中的集合,标志性符号是花括号{}

集合与字典类似,集合中的元素被看作是字典当中的键,集合中没有重复值

1.定义集合

1.1 方法一:通过花括号(和逗号)定义集合

python   符号 集合python符号_python   符号

 

 1.2方法二 通过set 函数将其他数据类型转换成集合

python   符号 集合python符号_并集_02

 

 2.集合常用用来对其他数据类型进行去重

两个项目所有同事冰河到一个列表中(其并集或者借用列表、元组等)

python   符号 集合python符号_数据类型转换_03

 

 set 不可直接+ 求并 ,可以用|求并集,或者转化成list后用+ 求并 后用set转化进行去重

python   符号 集合python符号_数据类型_04

 

 3.集合的常用操作

python   符号 集合python符号_数据类型_05

 

 

python   符号 集合python符号_数据类型_06

 

 3.1增 add

python   符号 集合python符号_数据类型转换_07

 

 3.2删 pop 随机删,set1.remove("猪八戒") 按照元素值删除,set1.clear() 清空

方式一 pop 随机删

python   符号 集合python符号_数据类型_08

 

 方式二,按照元素值删除

python   符号 集合python符号_数据类型转换_09

 

 方式三 清空

python   符号 集合python符号_数据类型转换_10

 

 3.3改   ,更新update  通过一个集合更新另一个集合

python   符号 集合python符号_数据类型转换_11

 

 3.4查 in

python   符号 集合python符号_python   符号_12

 

 3.5专属于集合的操作

并集 |     并集不会对原数据进行修改,而update 会对原数据进行更改

python   符号 集合python符号_python   符号_13

 

 交集 &:两个共有的元素

python   符号 集合python符号_数据类型_14

 

 差集 -

python   符号 集合python符号_数据类型_15

 

对称差分 ^: 两个集合的并集减掉两个集合的交集,两个项目组中只负责一个项目的成员

python   符号 集合python符号_数据类型_16

 

子集<

 

python   符号 集合python符号_并集_17