zoukankan      html  css  js  c++  java
  • python 的集合

    set  的特点: 把不同的元素组合在一起,元素值必须是可哈希不可变的 

    set 的创建

    s = set ('alex li')
    print(s)
    
    表现形式;去重   
    {'e', 'i', ' ', 'l', 'a', 'x'}
    

    set集合去重

    sy = ['cx','chenxi','cx']
    print(sy)
    print(set(sy))
    
    
    ['cx', 'chenxi', 'cx']
    {'chenxi', 'cx'}
    

      set 去重并查看数据类型

    sy = ['cx','chenxi','cx']
    print(sy)
    print(set(sy),type(set(sy)))
    
    
    
    
    {'i', 'a', 'e', 'x', ' ', 'l'}
    ['cx', 'chenxi', 'cx']
    {'chenxi', 'cx'} <class 'set'>
    

      set 把不同的元素组合到一起,但元素的值必须不能变化

    li = [[1,2],3,'tyuio']
    a = set(li)
    print(a)
    
    
    
    Traceback (most recent call last):
      File "D:/python/map.py", line 308, in <module>
        a = set(li)
    TypeError: unhashable type: 'list'
    

      set集合分为可变集合和非可变集合;创建方式

    s1 = set('alvin')   # 可变
    
    s2 = frozenset('yuan')   #不可变
    print(s1,type(s1))
    print(s2,type(s2))
    
    
    
    测试
    {'i', 'v', 'l', 'a', 'n'} <class 'set'>
    frozenset({'n', 'y', 'u', 'a'}) <class 'frozenset'>
    

      集合的访问与判断集合里是否存在某元素

    s1 =set('alvin')
    print('a' in s1)    #判断a元素是否在集合里存在;存在为Ture,不存在False
    print('b' in s1)
    for i in s1:
        print(i)
    
    
    
    
    True
    False
    a
    l
    n
    i
    v
    

      集合里添加单个元素

    def df():   #查看元素内容的函数
        # print('a' in s1)  # 判断a元素是否在集合里存在;存在为Ture,不存在False
        # print('b' in s1)
        print("********************************")
        for i in s1:
            print(i)
        print('
    ---------
    ')
    
    a =[1,5,2,"wertayuio",6,5]
    s1 =set(a)
    df()
    s1.add("wsd")   #在元素里添加一个元素
    df()
    
    
    
    False
    False
    ********************************
    1
    2
    wertayuio
    5
    6
    ---------
    False
    False
    ********************************
    1
    wertayuio
    5
    6
    ---------
    

      更新(添加多个元素)元素

    s1.update("ops2") #作为一个序列添元素,如果重复只添加一个
    df()
    1
    2
    5
    6
    wertayuio
    ---------
    
    1
    2
    5
    6
    p
    wertayuio
    o
    s
    2
    ---------
    

      更新(添加多个元素)元素

    s1.update([12,'wwww'])  #如果列表只更新添加两个
    df()
    
    
    
    
    
    测试
    D:pythpython.exe D:/python/map.py
    wertayuio
    1
    2
    5
    6
    ---------
    
    wertayuio
    1
    2
    5
    6
    12
    wwww
    ---------
    

      删除集合指定元素操作

    s1.remove(2) #删除指定的元素操作
    df()
    
    
    
    1
    2
    5
    6
    wertayuio
    ---------
    
    1
    5
    6
    wertayuio
    ---------
    

      随机删除操作

    print(s1)
    s1.pop()
    print(s1)
    
    
    
    测试
    {1, 2, 5, 6, 'wertayuio'}
    {2, 5, 6, 'wertayuio'}
    

      清空集合内容

    print(s1)
    s1.clear()  #清空
    print(s1)
    
    {1, 2, 'wertayuio', 5, 6}
    set()
    

      判断两个集合是否相同

    s = set("chenxi")
    s1 = set("chenxixi")
    print(s==s1)   #判断两个集合是否相等,相等为True
    
    
    
    
    True
    

      判断集合1是否被集合2 包含

    print(set("qwert")<set("qwertyuiop"))  #判断后者集合是否包含前者
    
    
    
    True
    

      集合and与or

    print(set("12345") or set("567890"))  #打印前面的set集合
    print(set("12345") and set("567890")) # 打印后面的集合
    
    
    
    {'2', '4', '5', '1', '3'}
    {'8', '0', '5', '6', '9', '7'}
    

      集合的交集与并集

    s1 = set("123456")
    s2 = set("456789")
    print(s2.intersection(s1))  #取交集;两个集合共有的元素
    print(s2.union(s1)) #取并集,指示取两个集合全部的元素并去重
    print(s1 | s2)  #求并集的另一方法

    {'5', '4', '6'}
    {'7', '8', '5', '3', '2', '4', '1', '9', '6'}

    {'7', '8', '5', '3', '2', '4', '1', '9', '6'}

      取集合的差集

    s1 = set("123456")
    s2 = set("456789")
    
    print(s2.difference(s1)) #取s2里有的,s1里没有的元素
    
    print(s1.difference(s2))  # 取s1里有的,s2里没有的元素
    print(s1.symmetric_difference(s2)) #取对称差集
    {'7', '8', '9'}
    {'1', '3', '2'}
    {'7', '1', '9', '2', '8', '3'}

      

      

    草都可以从石头缝隙中长出来更可况你呢
  • 相关阅读:
    【转】DirectoryEntry.Properties属性的遍历
    mysql 插入优化
    MySQL错误无法启动1067
    用ADO.NET的ExecuteScalar方法返回单一值资讯动态
    poj 1416 Shredding Company
    poj 1724 ROADS
    poj 3411 Paid Roads
    poj 1129 Channel Allocation
    poj 2676 Sudoku
    spring里的applicationlisener
  • 原文地址:https://www.cnblogs.com/rdchenxi/p/12036967.html
Copyright © 2011-2022 走看看