集合类型内置方法
集合类型内置方法
一、集合类型内置方法(set)
集合可以理解成一个集合体,学习python的学生可以是一个集合体;学习linux的学生可以是一个集合体。
pythoners = ['jason','nick','tank','sean']
linuxers = ['nick','egon','kevin']
#即报名python的学生又报名linuxe的学生
py_li_list = []
for stu in pythoners:
if stu in linuxers:
py_li_list.append(stu)
print(py_li_list)
['nick']
上述的列表方式求两个集合体的关系运算非常复杂,因此有了我们的集合数据类型。
- 用途:用于关系运算的集合体,由于集合内的元素无序企鹅集合元素不可重复,因此几个可以去重,但是去重后的几个会打乱原来元素的顺序。
- 定义:{}内用逗号分隔开多个元素,每个元素必须是不可变类型。
s = {1,2,1,'a'} # s = set({1,2,'a'})
print()
{1, 2, 'a'}
s = {1,2,1,'a','c'}
for i in s:
print(i)
1 2 a c
s = set('hello')
print(s)
{'h', 'e', 'o', 'l'}
- 常用操作+内置方法:常用操作和内置方法分为优先掌握、需要掌握两个部分。
二、优先掌握
- 长度len
- 成员运算in和not in
- | 并集、union
- &交集、intersection
- -差集、difference
- ^对称差集、symmetric_difference
- ==(判断是否相等)
- 父集:>、>=、issuperset
- 子集:<、<=、issubset
1.长度len
# set之长度len
a = {1,2,'a'}
print(len(a))
3
- 成员运算in和not in
# set之成员运算in和not in
s ={1,2,'a'}
print(l in s)
True
- |并集
# str之|并集
pythoners = {'jason','nick','tank','sean'}
linuxers = {'nick','egon','kevin'}
print(pythoners|linuxers)
print(pythoners.union(linuxers))
{'kevin', 'egon', 'tank', 'sean', 'jason', 'nick'} {'kevin', 'egon', 'tank', 'sean', 'jason', 'nick'}
- &交集
# str之&交集
pythoners = {'jason','nick','tank','sean'}
linuxers = {'nick','egon','kevin'}
print(pythoners&linuxers)
print(pythoners.intersection(linuers))
{'nick'} {'nick'}
- -差集
# str之-差集
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
print(pythoners-linuxers)
print(pythoners.difference(linuxers))
{'jason', 'tank', 'sean'} {'jason', 'tank', 'sean'}
- ^对称差集
# str之^对称差集
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
print(pythoners^linuxers)
print(pythoners.symmetric_difference(linuxers))
{'sean', 'egon', 'tank', 'kevin', 'jason'} {'sean', 'egon', 'tank', 'kevin', 'jason'}
- ==
# str之==
pythoners = {'jason','nick','tank','sean'}
linuxers = {'nick','egon','kevin'}
javers = {'nick','egon','kevin'}
print(pythoners == linuxers)
print(linuxers == javers)
· False
True ·
- 父集:>,>=
# str之父集:>,>=
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
javaers = {'jason', 'nick'}
print(pythoners>linuxers)
print(pythoners>=linuxers)
print(pythoners>=javaers)
print(pythoners.insuperset(javaers))
False False True True
- 子集:<、<=
# str之子集:<、<=
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
javaers = {'jason', 'nick'}
print(pythoners<linuxers)
print(pythoners<=linuxers)
print(javaers.issubset(javaers))
False False True
三、需要掌握
- add
- remove
- difference_update
- discard
- isdisjoint
1.add()
# set之add()![](https://img2018.cnblogs.com/blog/1731480/201908/1731480-20190805221154757-537199860.jpg)
2. remove
```python
# set之remove()
s = {1,2,'a'}
s.remove(1)
print(s)
{2, 'a'}
- difference_update
# str之difference_update
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
pythoners.difference_update(linuxers)
print(pythoners)
{'jason', 'sean', 'tank'}
- discard()
# set之discard()
s = {1,2,'a'}
# s.remove(3)
s.discard(3)
print(s)
{'a', 1, 2}
- isdisjoint()
#set之isdisjoint(),集合没有共同的步伐返回True,否则返回False
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
pythoners.isdisjoint(linuxers)
print(pythoners.isdisjoint(linuxers))
False