5.【Python编程】集合Set相关知识及基本操作
备注: 本教程主要使用Python3.6在jupyter notebook上编程实现。Python环境配置参考《【Python学习】Windows10开始你的Anaconda安装与Python环境管理》或者《【Python学习】纯终端命令开始你的Anaconda安装与Python环境管理》。
5.1 集合的创建
集合(set)是一个无序的不重复元素序列。可以使用大括号{ } 或者set() 函数创建集合,注意:创建一个空集合必须用set() 而不是{ } ,因为{ } 是用来创建一个空字典。此外,集合中的元素必须是不可变类型。 python3版本,对于数据量很小的集合并且数字很少的时候,确实是做了一个排序,但集合的本质还是无序的! Python内置的集合set中元素顺序是按元素的哈希值进行存储的,并不是按先后顺序。
set1 = {"北京","上海","广州","深圳","成都"}
print(set1)
set2 = set()
print(set2)
list1 = [1,1,1,2,2,3,4,5,6,7,8,8,9,10]
set3 = set(list1)
print(set3)
{'成都', '深圳', '上海', '广州', '北京'}
set()
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
set4 = set([9,4,1,6,100,101,119,222,333,301,1000,1301,200])
print(set4)
set4_2 = set([9,4,1,6,100,101,119,222,333,301,1000,1301,200])
print(set4_2)
set5 = set([4,9,1,6,222,101,119,100,333,301,1000,1301,200])
print(set5)
{1, 100, 4, 6, 101, 1000, 9, 200, 333, 301, 1301, 119, 222}
{1, 100, 4, 6, 101, 1000, 9, 200, 333, 301, 1301, 119, 222}
{1, 4, 101, 6, 100, 1000, 9, 200, 333, 301, 1301, 119, 222}
5.2 集合的常见操作
set1 = {x for x in '我是中国人,中国人不骗中国人' if x not in '我是谁'}
print(set1)
{',', '国', '中', '骗', '不', '人'}
empty_set = set()
print("len(empty_set)=", len(empty_set))
set1 = {1,3,5,7,9}
print("len(set1)=", len(set1))
len(empty_set)= 0
len(set1)= 5
set1 = {1,3,5,7,9}
print(set1)
set1.clear()
print("执行set1.clear()之后...")
print(set1)
{1, 3, 5, 7, 9}
执行set1.clear()之后...
set()
A = {1,2,3,4,5,6}
print(A)
A.add(1)
print(A)
A.add(7)
print(A)
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6, 7}
A.update(x) 可以添加元素,且参数可以是列表,元组,字典等
A = {1,2,3,4,5,6}
A.update([1,3],[8,9])
print(A)
A.update((11,12,13))
print(A)
A.update({15,16,17})
print(A)
A.update({18:19,20:21})
print(A)
{1, 2, 3, 4, 5, 6, 8, 9}
{1, 2, 3, 4, 5, 6, 8, 9, 11, 12, 13}
{1, 2, 3, 4, 5, 6, 8, 9, 11, 12, 13, 15, 16, 17}
{1, 2, 3, 4, 5, 6, 8, 9, 11, 12, 13, 15, 16, 17, 18, 20}
A = {1,2,3,4,5,6}
A.remove(1)
'''
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-26-d5cf803bb5a2> in <module>
1 A = {1,2,3,4,5,6}
2 A.remove(1)
----> 3 A.remove(7) # 元素必须在集合当中,否则会报错如下:
KeyError: 7
'''
print(A)
A.discard(2)
A.discard(8)
print(A)
{2, 3, 4, 5, 6}
{3, 4, 5, 6}
集合里只有0~31范围内的数字,执行.pop() 删除时, 删掉的是最小的数字, 其余数字升序排列 实际上,.pop 执行的是删除实际存储在集合当中最前面的元素
set1 = {9,31,4,1,6,21,8,3,11,0,21,24,25,29}
print("set1:",set1)
set1.pop()
print("set1:",set1)
set1.pop()
print("set1:",set1)
set1.pop()
print("set1:",set1)
set2 = {9,4,1,6,100,101,119,222,333,301,1000,1301,200}
print("set2:",set2)
set2.pop()
print("set2:",set2)
set2.pop()
print("set2:",set2)
set2.pop()
print("set2:",set2)
set3= set('我是中国人')
print("set3:",set3)
set3.pop()
print("set3:",set3)
set3.pop()
print("set3:",set3)
set3.pop()
print("set3:",set3)
set1: {0, 1, 3, 4, 6, 8, 9, 11, 21, 24, 25, 29, 31}
set1: {1, 3, 4, 6, 8, 9, 11, 21, 24, 25, 29, 31}
set1: {3, 4, 6, 8, 9, 11, 21, 24, 25, 29, 31}
set1: {4, 6, 8, 9, 11, 21, 24, 25, 29, 31}
set2: {1, 100, 4, 6, 101, 1000, 9, 200, 333, 301, 1301, 119, 222}
set2: {100, 4, 6, 101, 1000, 9, 200, 333, 301, 1301, 119, 222}
set2: {4, 6, 101, 1000, 9, 200, 333, 301, 1301, 119, 222}
set2: {6, 101, 1000, 9, 200, 333, 301, 1301, 119, 222}
set3: {'国', '中', '是', '我', '人'}
set3: {'中', '是', '我', '人'}
set3: {'是', '我', '人'}
set3: {'我', '人'}
5.3 集合的基本运算
set() 与list() 的区别
sentence_set = set('我是中国人,中国人不骗中国人')
print(sentence_set)
print('句子中的不同字符个数:',len(sentence_set))
sentence_list = list('我是中国人,中国人不骗中国人')
print(sentence_list)
print('句子的长度:',len(sentence_list))
{'我', ',', '国', '骗', '是', '人', '中', '不'}
8
['我', '是', '中', '国', '人', ',', '中', '国', '人', '不', '骗', '中', '国', '人']
两个集合的基本运算
A = {1,2,3,4}
B = {3,4,5,6}
print("集合A与B的并集:",A | B)
C = A.union(B)
print("集合A与B的并集:", C)
集合A与B的并集: {1, 2, 3, 4, 5, 6}
集合A与B的并集: {1, 2, 3, 4, 5, 6}
A = {1,2,3,4}
B = {3,4,5,6}
print("集合A与B的交集:",A & B)
D = A.intersection(B)
print("集合A与B的交集:", D)
集合A与B的交集: {3, 4}
集合A与B的交集: {3, 4}
A = {1,2,3,4}
B = {3,4,5,6}
print("集合A与B的差运算:",A - B)
E = A.difference(B)
print("集合A与B的差运算",E)
集合A与B的差运算: {1, 2}
集合A与B的差运算 {1, 2}
A = {1,2,3,4}
B = {3,4,5,6}
print("集合A与B中不同时存在的元素:",A ^ B)
F = A.symmetric_difference(B)
print("集合A与B中不同时存在的元素:",F)
print("集合A与B中不同时存在的元素:",(A-(A&B))|(B-(A&B)))
集合A与B中不同时存在的元素: {1, 2, 5, 6}
集合A与B中不同时存在的元素: {1, 2, 5, 6}
集合A与B中不同时存在的元素: {1, 2, 5, 6}
5.4 元素与集合的关系
A = {1,2,3,4,5,6,7}
print(1 in A)
print(8 in A)
True
False
|