集合的内置方法
set(集合)
集合可以理解成一个集合体。
pythons = ['jason', 'nick', 'tank', 'sean']linuxs = ['nick', 'egon', 'kevin']py_lin_list =[]for stu in pythons: if stu in linuxs: py_lin_list.append(stu)print(f"pythons and linuxs:{py_lin_list}")运行结果为:pythons and linuxs:['nick']
1、用途:用于关系运算的集合体,由于集合内的元素无序且集合元素不可重复,因此集合可以去重,但是去重后的集合会打乱原来元素的顺序。
s = {1, 2, 1, 'a'} # s = set({1,2,'a'}) #集合将重复的元素默认进行了去重操作print(f"s: {s}")运行结果为:s: {'a', 1, 2}
s = {1, 2, 1, 'a', 'c'}for i in s: print(i) 运行结果为:12ac
s = set('hello')print(f"s: {s}")运行结果为:s: {'h', 'l', 'e', 'o'}
2、定义:{}内用逗号分隔开多个元素,每个元素必须是不可变类型,不可哈希就是可变,可哈希就是不可变
3、常用操作+内置方法:常用操作和内置方法分为优先掌握(今天必须得记住)、需要掌握(一周内记住)两个部分。
1. 长度len
2. 成员运算in和not in
3. |并集、union4. &交集、intersection5. -差集、difference6. ^对称差集、symmetric_difference7. ==8. 父集:>、>= 、issuperset9. 子集:<、<= 、issubset1.长度len
s = {1, 2, 'a'}print(f'len(s):{len(s)}')运行结果:len(s):3
2.成员运算in和not in
s = {1, 2, 'a'}print(f'1 not in s:{1 not in s}')print(f"'b' in s:{'b' in s}")运行结果为:1 not in s:False'b' in s:False
3.|并集、union
pythoners = {'jason', 'nick', 'tank', 'sean'}linuxers = {'nick', 'egon', 'kevin'}wer = pythoners|linuxersprint(f'wer;{wer}')运行结果为:wer;{'sean', 'tank', 'kevin', 'egon', 'nick', 'jason'}
4.&交集、intersection
pythoners = {'jason', 'nick', 'tank', 'sean'}linuxers = {'nick', 'egon', 'kevin'}wer = pythoners&linuxersprint(f'wer;{wer}')运行结果:wer;{'nick'}
5.-差集、difference(移除A集合中与B集合相同的元素)
pythoners = {'jason', 'nick', 'tank', 'sean'}linuxers = {'nick', 'egon', 'kevin'}wer = pythoners-linuxersprint(f'wer;{wer}')运行结果为:wer;{'jason', 'tank', 'sean'}
6.^对称差集、symmetric_difference(删除A集合与B集合相同的元素,并组成新的集合)
pythoners = {'jason', 'nick', 'tank', 'sean'}linuxers = {'nick', 'egon', 'kevin'}wer = pythoners^linuxersprint(f'wer;{wer}')运行结果为:wer;{'jason', 'tank', 'sean', 'egon', 'kevin'}
7.==(两边集合是否相等)
pythoners = {'jason', 'nick', 'tank', 'sean'}linuxers = {'nick', 'egon', 'kevin'}javers = {'nick', 'egon', 'kevin'}print(f'pythoners==linuxers;{pythoners==linuxers}')print(f"javers==linuxers: {javers==linuxers}")运行结果为:pythoners==linuxers;Falsejavers==linuxers: True
8.父集:>、>= 、issuperset(判断A集合是否包含或等于B集合,则称A集合为B集合的父集)
pythoners = {'jason', 'nick', 'tank', 'sean'}linuxers = {'nick', 'egon', 'kevin'}javaers = {'jason', 'nick'}print(f"pythoners>linuxers: {pythoners>linuxers}")print(f"pythoners>=linuxers: {pythoners>=linuxers}")print(f"pythoners>=javaers: {pythoners>=javaers}")print(f"pythoners.issuperset(javaers): {pythoners.issuperset(javaers)}")运行结果为:pythoners>linuxers: Falsepythoners>=linuxers: Falsepythoners>=javaers: Truepythoners.issuperset(javaers): True
9.子集:<、<=(判断A集合是否属于或等于B集合,则称A集合为B集合的子集)
pythoners = {'jason', 'nick', 'tank', 'sean'}linuxers = {'nick', 'egon', 'kevin'}javaers = {'jason', 'nick'}print(f"pythoners
需要掌握(****)
- add
- remove
- difference_update
- discard
- isdisjoint
1.add(添加元素)
s = {1, 2, 'a'}s.add(8) # 添加元素print(s)运行结果为;{8, 1, 2, 'a'}
2.remove(移除元素)
s = {1, 2, 'a'}s.add(8) # 添加元素print(s)s.remove('a') # 移除元素,元素不存在则报错print(s){8, 1, 2, 'a'}{8, 1, 2}
3.difference_update(移除A集合中与B集合相同的元素)
pythoners = {'jason', 'nick', 'tank', 'sean'}linuxers = {'nick', 'egon', 'kevin'}pythoners.difference_update(linuxers) #移除A集合中与B集合相同的元素print(pythoners){'tank', 'jason', 'sean'}
4.isdisjoint (判断两个集合没有相同的元素)
pythoners = {'jason', 'nick', 'tank', 'sean'}linuxers = {'nick', 'egon', 'kevin'}print(pythoners.isdisjoint(linuxers)) 集合没有相同的元素返回True,有相同的元素返回FalseFalse
5.discard(移除指定的元素)
pythoners = {'jason', 'nick', 'tank', 'sean'}pythoners.discard('nick') #移除指定的元素,元素不存在,返回Noneprint(pythoners){'sean', 'jason', 'tank'}
4、存一个值or多个值:多个值,且值为不可变类型。
5、有序or无序:无序,无索引
6、可变or不可变:可变数据类型(变量值改变,ID内存地址不变)
s = {1, 2, 'a'}print(f"第一个id {id(s)}")s.add(6)print(f"第二个id {id(s)}")