集合类型内置方法

集合类型内置方法

一、集合类型内置方法(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']

上述的列表方式求两个集合体的关系运算非常复杂,因此有了我们的集合数据类型。

  1. 用途:用于关系运算的集合体,由于集合内的元素无序企鹅集合元素不可重复,因此几个可以去重,但是去重后的几个会打乱原来元素的顺序。
  2. 定义:{}内用逗号分隔开多个元素,每个元素必须是不可变类型。
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'}

  1. 常用操作+内置方法:常用操作和内置方法分为优先掌握、需要掌握两个部分。

二、优先掌握

  1. 长度len
  2. 成员运算in和not in
  3. | 并集、union
  4. &交集、intersection
  5. -差集、difference
  6. ^对称差集、symmetric_difference
  7. ==(判断是否相等)
  8. 父集:>、>=、issuperset
  9. 子集:<、<=、issubset

1.长度len

# set之长度len
a = {1,2,'a'}
print(len(a))

3

  1. 成员运算in和not in
# set之成员运算in和not in
s ={1,2,'a'}
print(l in s)

True

  1. |并集
# 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'}

  1. &交集
# str之&交集
pythoners = {'jason','nick','tank','sean'}
linuxers = {'nick','egon','kevin'}
 print(pythoners&linuxers)
  print(pythoners.intersection(linuers))

{'nick'} {'nick'}

  1. -差集
# str之-差集
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}

print(pythoners-linuxers)
print(pythoners.difference(linuxers))

{'jason', 'tank', 'sean'} {'jason', 'tank', 'sean'}

  1. ^对称差集
# 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'}

  1. ==
# str之==
pythoners = {'jason','nick','tank','sean'}
linuxers = {'nick','egon','kevin'}
javers = {'nick','egon','kevin'}

print(pythoners == linuxers)
print(linuxers == javers)

· False
True ·

  1. 父集:>,>=
# 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

  1. 子集:<、<=
# 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

三、需要掌握

  1. add
  2. remove
  3. difference_update
  4. discard
  5. 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'}

  1. difference_update
# str之difference_update
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
pythoners.difference_update(linuxers)

print(pythoners)

{'jason', 'sean', 'tank'}

  1. discard()
# set之discard()
s = {1,2,'a'}
# s.remove(3)
s.discard(3)
print(s)

{'a', 1, 2}

  1. isdisjoint()
#set之isdisjoint(),集合没有共同的步伐返回True,否则返回False
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
pythoners.isdisjoint(linuxers)

print(pythoners.isdisjoint(linuxers))

False

posted @ 2019-08-05 22:12  Daoqi  阅读(277)  评论(0编辑  收藏  举报