集合类型内置方法
一、 集合类型内置方法(set)
集合可以理解成一个集合体,学习Python的学生可以是一个集合体;学习Linux的学生可以是一个集合体。
pythoners = ['lh', 'nick', 'hello', 'word']
linuxers = ['nick', 'handsome', 'hi']
# 即报名pythoners又报名lniuxers学生
py_li_list = []
for stu in pythoners:
if stu in linuxers:
py_li_list.append(stu)
print(f"pythoners and linuxers: {py_li_list}")
pythoners and linuxers: ['nick']
上述的列表方式求两个集合体的关系运算非常复杂,因此有了我们的集合数据类型。
1.用途:用于关系运算的集合体,由于集合内的元素无序且集合元素不可重复,因此集合可以去重,但是去重后的集合会打乱原来元素的顺序。
2.定义:{}内用逗号分隔开多个元素,每个元素必须是不可变类型。
s = {1, 2, 1, 'a'}
print(f"s: {s}")
s: {1, 2, 'a'}
s = {1, 2, 1, 'a', 'c'}
for i in s:
print(i)
c
1
2
a
s = set('hello')
print(f"s: {s}")
s: {'h', 'e', 'o', 'l'}
3.常用操作+内置方法:常用操作和内置方法分为优先掌握(今天必须得记住)、需要掌握(一周内记住)两个部分。
1.1优先掌握(*************)
- 长度len
- 成员运算in和not in
- |并集、union
- &交集、intersection
- -差集、difference
- ^对称差集、symmetric_difference
- ==
- 父集:>、>= 、issuperset
- 子集:<、<= 、issubset
1.长度len
s = {1, 2, 'a'}
print(f"len(s): {len(s)}")
len(s): 3
2.成员运算in和not in
# set之成员运算in和not in
s = {1, 2, 'a'}
print(f"1 in s: {1 in s}")
1 in s: True
3.|并集
# str之|并集
pythoners = {'lh', 'nick', 'hello', 'handsome'}
linuxers = {'nick', 'word', 'hi'}
print(f"pythoners|linuxers:{pythoners | linuxers}")
print(f"pythoners.union(linuxers): {pythoners.union(linuxers)}")
pythoners|linuxers:{'lh', 'hello', 'hi', 'nick', 'word', 'handsome'}
pythoners.union(linuxers): {'lh', 'hello', 'hi', 'nick', 'word', 'handsome'}
4.&交集
# str之&交集
pythoners = {'lh', 'nick', 'hello', 'handsome'}
linuxers = {'nick', 'word', 'hi'}
print(f"pythoners-linuxers:{pythoners & linuxers}")
print(f"pythoners.intersection(linuxers): {pythoners.intersection(linuxers)}")
pythoners-linuxers:{'nick'}
pythoners.intersection(linuxers): {'nick'}
5.-差集
# str之-差集
pythoners = {'lh', 'nick', 'hello', 'handsome'}
linuxers = {'nick', 'word', 'hi'}
print(f"pythoners-linuxers:{pythoners - linuxers}")
print(f"pythoners.difference(linuxers): {pythoners.difference(linuxers)}")
pythoners-linuxers:{'handsome', 'lh', 'hello'}
pythoners.difference(linuxers): {'handsome', 'lh', 'hello'}
6.^对称差集
# str之^对称差集
pythoners = {'lh', 'nick', 'hello', 'handsome'}
linuxers = {'nick', 'word', 'hi'}
print(f"pythoners^linuxers:{pythoners ^ linuxers}")
print(f"pythoners.symmetric_difference(linuxers): {pythoners.symmetric_difference(linuxers)}")
pythoners^linuxers:{'word', 'hi', 'handsome', 'lh', 'hello'}
pythoners.symmetric_difference(linuxers): {'word', 'hi', 'handsome', 'lh', 'hello'}
7.==
# str之==
pythoners = {'lh', 'nick', 'hello', 'handsome'}
linuxers = {'nick', 'word', 'hi'}
javers = {'nick', 'word', 'hi'}
print(f"pythoners==linuxers:{pythoners == linuxers}")
print(f"javers==linuxers: {javers == linuxers}")
pythonerslinuxers:False
javerslinuxers: True
8.父集:>、>=
# str之父集:>、>=
pythoners = {'lh', 'nick', 'hello', 'handsome'}
linuxers = {'nick', 'word', 'hi'}
javaers = {'nick', 'lh'}
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: False
pythoners>=linuxers: False
pythoners>=javaers: True
pythoners.issuperset(javaers): True
9.子集:<、<=
# str之子集:<、<=
pythoners = {'lh', 'nick', 'hello', 'handsome'}
linuxers = {'nick', 'word', 'hi'}
javaers = {'nick', 'lh'}
print(f"pythoners<linuxers: {pythoners < linuxers}")
print(f"pythoners<=linuxers: {pythoners <= linuxers}")
print(f"javaers.issubset(javaers): {javaers.issubset(javaers)}")
pythoners<linuxers: False
pythoners<=linuxers: False
javaers.issubset(javaers): True
1.2需要掌握(************)
- add
- remove
- difference_update
- discard
- isdisjoint
1.add
# set之add()
s = {1, 2, 'a'}
s.add(3)
print(s)
{1, 2, 3, 'a'}
2.remove()
# set之remove()
s = {1, 2, 'a'}
s.remove(1)
print(s)
{2, 'a'}
3.difference_update()
# str之difference_update()
pythoners = {'lh', 'nick', 'hello', 'handsome'}
linuxers = {'nick', 'word', 'hi'}
pythoners.difference_update(linuxers)
print(f"pythoners.difference_update(linuxers): {pythoners}")
pythoners.difference_update(linuxers): {'lh', 'hello', 'handsome'}
4.discard()
# set之discard()
s = {1, 2, 'a'}
# remove(3) # 报错
s.discard(3)
print(s)
{1, 2, 'a'}
5.isdisjoint()
# set之isdisjoint(),集合没有共同的部分返回True,否则返回False
pythoners = {'lh', 'nick', 'hello', 'handsome'}
linuxers = {'nick', 'word', 'hi'}
pythoners.isdisjoint(linuxers)
print(f"pythoners.isdisjoint(linuxers): {pythoners.isdisjoint(linuxers)}")
pythoners.isdisjoint(linuxers): False