学习内容:集合类型内置方法

四、集合类型内置方法

set(掌握)

集合可以理解成一个集合体,学习Python的学生可以是一个集合体;学习linux的学生可以是一个集合体。

```
pythoners = ['jason','nick','tank','sean']
linuxers = ['nick','egon','kevin']
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,3,'a'}
    print(f's:{s}')
    
    
    s:{'a', 1, 2, 3}
    s = {1,2,3,'a'}
    for i in s:
        print(i)
    
    
    1
    2
    3
    a
    s = set('hello')
    print(f's:{s}')
    
    
    s:{'h', 'e', 'l', 'o'}

3. 常用操作+内置方法:常用操作和内置方法分为优先掌握(今天必须得记住)、需要掌握(一周内记住)两个部分。

优先掌握

  1. 长度len

    s = {1,2,'jin'}
    print(len(s))
    
    
    3
  2. 成员运算in和not in

    s = {1,2,'jin'}
    print('jin' in s)
    print('jin' not in s)
    
    
    True
    False
  3. |并集、union 将多个集合当中的值组合在一起,且相同的值不会重复

    game = {'lol','dnf','qq','sp'}
    software = {'qq','sp','xt'}
    n = {1,2,3,4}
    print(game|software|n)
    print(game.union(software,n))
    
    
    {1, 'sp', 'xt', 2, 3, 4, 'dnf', 'qq', 'lol'}
    {1, 'sp', 'xt', 2, 3, 4, 'dnf', 'qq', 'lol'}
  4. &交集、intersection

    game = {'lol','dnf','qq','sp'}
    software = {'qq','sp','xt'}
    
    print(game&software)
    print(game.intersection(software))
    
    
    {'qq', 'sp'}
    {'qq', 'sp'}
  5. -差集、difference

    game = {'lol','dnf','qq','sp'}
    software = {'qq','sp','xt'}
    
    print(game-software)
    print(software-game)
    print(game.difference(software))
    print(software.difference(game))
    
    
    {'lol', 'dnf'}
    {'xt'}
    {'lol', 'dnf'}
    {'xt'}
  6. ^对称差集、symmetric_difference

    game = {'lol','dnf','qq','sp'}
    software = {'qq','sp','xt'}
    
    print(game^software)
    print(software^game)
    print(game.symmetric_difference(software))
    print(software.symmetric_difference(game))
    
    
    {'xt', 'lol', 'dnf'}
    {'lol', 'xt', 'dnf'}
    {'xt', 'lol', 'dnf'}
    {'lol', 'xt', 'dnf'}
  7. ==

    game = {'lol','dnf','qq','sp'}
    software = {'qq','sp','xt'}
    chengxu = {'lol','qq','sp','dnf'}
    print(game == software)
    print(game == chengxu)
    
    
    False
    True
  8. 父集:>、>= 、issuperset

    game = {'lol','dnf','qq','sp'}
    software = {'qq','sp','xt'}
    chengxu = {'lol','qq'}
    print(game > software)
    print(game >= software)
    print(game.issuperset(software))
    print(game > chengxu)
    print(game >= chengxu)
    print(game.issuperset(chengxu))
    
    
    False
    False
    False
    True
    True
    True
  9. 子集:<、<=

    game = {'lol','dnf','qq','sp'}
    software = {'qq','sp','xt'}
    chengxu = {'lol','qq'}
    print(game < software)
    print(software < game)
    print(game <= software)
    print(game.issuperset(software))
    print(software.issuperset(game))
    print(game < chengxu)
    print(chengxu < game)
    print(game <= chengxu)
    print(chengxu <= game)
    print(game.issuperset(chengxu))
    print(chengxu.issuperset(game))
    
    
    False
    False
    False
    False
    False
    False
    True
    False
    True
    True
    False

    需要掌握

    1. add 添加元素
    n = {1,2,3,'a'}
    n.add(4)
    print(n)
    
    
    {'a', 1, 2, 3, 4}
    1. remove 移除元素,元素不存在则报错
    n = {1,2,3,'a'}
    n.remove(2)
    print(n)
    
    
    {1, 3, 'a'}
    1. difference_update 相同更新, 值相同的去除,值不同的打印出来
    n = {1,2,3,6,'a'}
    a = {2,4,'a'}
    n.difference_update(a)
    print(n)
    
    
    
    {1, 3,6}
    1. discard 丢弃
    n = {1,2,3,6,'a'}
    n.discard(3)
    print(n)
    
    
    {1, 2, 6, 'a'}
    1. isdisjoint 集合没有共同的部分返回True,否者返回False
    n = {1,2,3,6,'a'}
    a = {2,4,'a'}
    print(n.isdisjoint(a))
    
    
    False
  10. 存一个值or多个值:多个值,且值为不可变类型。
  11. 有序or无序:无序
  12. 可变or不可变:可变数据类型

猜你喜欢

转载自www.cnblogs.com/jincoco/p/10923638.html
0条评论
添加一条新回复