zoukankan      html  css  js  c++  java
  • set集合

    (集合可以看做是字典去掉value由key组成的)

    1:数据元素唯一

    2:无序

    3:不可变

    集合的应用场景:

    如果说之后做爬虫的时候,首先你是不是获取到连接,(递归),是不是同一个连接你不能重复去爬取下载

    去重 ,我们就可以放到集合中

    定义:

    s=set('xiaofan')
    print(s)

    {'i', 'n', 'f', 'o', 'x', 'a'}

    s1=set(['a','b','c'])
    print(s1)

    {'b', 'a', 'c'}

    添加:

    s.add("xiaofan")
    print(s)

    {'i', 'n', 'xiaofan', 'f', 'o', 'x', 'a'}


    s.update("TT")
    print(s)

    {'i', 'n', 'xiaofan', 'T', 'f', 'o', 'x', 'a'}

    删除:

    s.remove("xiaofan")
    print(s)

    {'i', 'n', 'T', 'f', 'o', 'x', 'a'}

    s.pop()
    print(s)

    {'i', 'a', 'x', 'T', 'o', 'f'}

    ------------------------------------

    s2=set('abc')
    s3=set('bcd')

    交集:
    print(s2|s3)

    {'d', 'a', 'b', 'c'}

    并集:
    print(s2&s3)

    {'b', 'c'}

    补差:s2中出去s3中含有的
    print(s2-s3)

    {'a'}

    对称差分:去掉集合中交集部分
    print(s2^s3)

    {'d', 'a'}

  • 相关阅读:
    Python_反射
    Python_面向对象_类2
    Python_面向对象_类1
    Python_logging模块
    Python_子进程管理subprocess模块
    Python_python内置加密模块
    Python_configparser模块
    Python_xml
    Python_shelve模块
    Python_shutil模块
  • 原文地址:https://www.cnblogs.com/fanxuanhui-linux/p/5876473.html
Copyright © 2011-2022 走看看