zoukankan      html  css  js  c++  java
  • python基础语法之集合set

    1 概念

      集合是一个无需的,不重复的数组组合,它的主要作用如下:

        去重,将一个列表装换成集合,会将其去重

        关系测试,测试两组数据的交集,差集,并集等关系。

      集合对象是一组无需排列的可哈希的值,集合成员可以作为字典的键。

      集合中的元素不可以是列表或字典

    2 集合的创建

    1 s1 = set('hello word')
    2 print(s1)
    3 # {'o', 'e', 'h', 'w', 'd', 'l', ' ', 'r'}
    4 
    5 s2 = set(['hello', 'word'])
    6 print(s2)
    7 # {'word', 'hello'}

     

    3 集合的访问

      由于集合是无序的,所以不能为集合创建索引或切片操作,只能循环遍历或使用in 、not in来访问判断集合元素

     1 s = set('hello word')
     2 print('h' in s)  # True
     3 print('a' in s)  # False
     4 for i in s:
     5     print(i)
     6 # l
     7 # d
     8 # w
     9 # h
    10 # r
    11 # e
    12 #
    13 # o

     

    4 集合的更新

      可以用以下方法来更新集合

      s.add()

      s.update()

      s.remove()

      del: 可以删除集合本身

     1 # 集合的更新
     2 s = set('hello word')
     3 s.add('mm')
     4 print(s)  # {'r', 'd', 'w', 'l', 'mm', 'h', 'o', ' ', 'e'}
     5 
     6 s.update('AB')
     7 print(s)  # {'h', 'o', 'B', 'd', 'e', 'mm', 'r', ' ', 'l', 'w', 'A'}
     8 
     9 s.remove('h')
    10 print(s)  # {'B', 'mm', 'd', 'e', 'o', 'A', ' ', 'w', 'r', 'l'}

      注意:

    1 s = set([1, 2, 'word'])
    2 s.update([12, 'aaa'])
    3 print(s)  # {1, 2, 12, 'word', 'aaa'}

    5 集合类型操作符

      1 等价与不等价(==, !=)

    1 s1 = set('hello')
    2 s2 = set('hellollo')
    3 print(s1 == s2)  # True

      2 子集,超集

    1 s1 = set('hello word')
    2 s2 = set('hello')
    3 print('h' in s1)  # True
    4 print(s2 < s1)  # True

      注意

    1 print(set('hello') < set('hellollo'))  # False

      3 并集(|)

        联合(union)操作与集合的or操作其实是等价的,联合符号有个等价的方法 union()

    1 s1 = set('hello')
    2 s2 = set('word')
    3 s3 = s1 | s2
    4 print(s3)  # {'w', 'o', 'r', 'e', 'd', 'l', 'h'}
    5 s4 = s1.union(s2)
    6 print(s4)  # {'w', 'o', 'r', 'e', 'd', 'l', 'h'}

      4 交集(&)

        与集合and等价,交集负号的等价方法是intersection()

    1 # 交集
    2 s1 = set('hello')
    3 s2 = set('word')
    4 s3 = s1 & s2
    5 print(s3)  # {'o'}
    6 s4 = s1.intersection(s2)
    7 print(s4)  # {'o'}

      5 差集(-)

        等价方法是difference()

    1 # 差集(-)
    2 s1 = set('hello')
    3 s2 = set('word')
    4 s3 = s1 - s2
    5 print(s3)  # {'e', 'h', 'l'}
    6 s4 = s1.difference(s2)
    7 print(s4)  # {'e', 'h', 'l'}

      6 对称差集(^)

        对称差集是集合的异或, 取得元素属于原来两个集合,但是不同时属于原来的两个集合,其等价方法是symmetric_difference()

    1 # 对称差集
    2 s1 = set('hello')
    3 s2 = set('world')
    4 s3 = s1 ^ s2
    5 print(s3)  # {'r', 'h', 'w', 'e', 'd'}
    6 s4 = s1.symmetric_difference(s2)
    7 print(s4)  # {'r', 'h', 'w', 'e', 'd'}

     

  • 相关阅读:
    powershell命令大全
    Android USB Connections Explained: MTP, PTP, and USB Mass Storage
    安装Windows Metasploit Framework
    Sublime Text2 jedi插件离线安装
    MySQL下载安装配置和Navicat for MySQL的安装配置
    Sublime中文编码问题
    Flask入门之结构重组(瘦身)-第13讲笔记
    Flask入门之SQLAlchemy配置与数据库连接
    Flask入门之flask-wtf表单处理
    Total Command使用笔记
  • 原文地址:https://www.cnblogs.com/swenwen/p/11277511.html
Copyright © 2011-2022 走看看