python路--集合

集合set

去重--常和列表结合

  a = [1,2,3,2,]

  set(a)  #得到去重的列表

  1#s = set('asdasd')   #{'a', 'd', 's'}

  2#print(set(1,2,3))集合参数只有一个

  3#s = [[1,2],'a'] 集合不能有列表

   #传入集合的只能是,数字,字符,元组。即绿色内容不能是可哈希的

 

     

    s = ['asd']

    a = set(s)

    a.add('uuu')

    print(a)  #{'uuu', 'asd'}

    a.update('123')   #{'asd', '3', '1', 'uuu', '2'}

    a.update('111')   #{'asd', '1', 'uuu', }

    a.update([12,'eee'])   #{'asd', 12,'eee', 'uuu', }

    #add加一个元素是方便的,加多个用元组。

    #update加单个数字,字符出现的效果不一般,加多个用列表--元组

  
        s.remove(内容)  

        s.clear()

        del s

  查  

    for循环

    迭代器

关系测试

    print( set('hello') == set('helloasd')  )  #元素分解,内容一致

    print( set('hello') != set('helloasd')  )

    print( set('hello') < set('hello world') )  #属于True

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

    print( set('hello') <= set('hello') )  #True

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

    a = set([1,2,3,4,5,])

    b = set([4,5,6,7,])

    #交集

    print(a.intersection(b))  #{4, 5}

    print(a & b)

    #并集

    print(a.union(b))  #{1, 2, 3, 4, 5, 6, 7}

    print(a|b)

    #差集

    print(a.difference(b))  #{1, 2, 3}

    print(a-b)

    #对称差集

    print(a.symmetric_difference(b))  #{1, 2, 3, 6, 7}

    print(a^b)

猜你喜欢

转载自www.cnblogs.com/5014sy/p/9773935.html