• python 集合总结


    '''
    集合:1:他是无序的,他是不重复的.
         2,他里面的元素必须是可哈希的. int str bool ()但是它本身是不可哈希的.
         3,集合不能更改里面的元素.
         4,集合可以求交集,并集,差集,反交集等.
    '''
    # lis = [1,1,2,2,3,3,3,4,5,6,6,7]
    #去重:
        #1,用算法去做.
        #2,转换成集合.在转换过来.
    # set1 = set(lis)
    # lis = list(set1)
    # print(lis)
    #创建集合:
    # set1 = set({1,2,'alex'})
    # set1 = {'alex','wusir','ritian','egon','barry'}
    #增:
    # print(set1)
    #add 添加一个元素=========================
    # set1.add('景女神')
    # print(set1)
    # #update 迭代着添加=======================
    # set1.update('abc')
    # set1.update([1,2,3])
    # print(set1)
    
    #删除
    #按照元素去删除============================
    # set1.remove('ritian')
    # print(set1)
    #随机删除 pop==========================
    # set1.pop()
    # print(set1)
    
    #清空集合 clear()=========================
    # set1.clear()
    # print(set1)
    #删除 集合==========================
    # del set1
    # print(set1)
    
    #查:
    # for i in set1:=========遍历查询集合===============
    #     print(i)
    
    #交集 &  intersection
    # set1 = {1,2,3,4,5}
    # set2 = {4,5,6,7,8}
    # set3 = set1 & set2
    # print(set3)
    # print(set1.intersection(set2))
    
    #并集 | union
    # print(set1 | set2)
    # print(set1.union(set2))
    #反交集 ^ symmetric_difference
    # print(set1 ^ set2)
    # print(set1.symmetric_difference(set2))
    
    #差集 -  difference
    # print(set1 - set2)
    # print(set2 - set1)
    # print(set1.difference(set2))
    set1 = {1,2,3}
    set2 = {1,2,3,4,5,6}
    #超集  <  issubset
    # print(set1 < set2)
    # print(set1.issubset(set2))
    # print(set1.issuperset(set2))
    # print(set2.issuperset(set1))
    
    # s2 = frozenset(set1)
    # print(s2,type(s2))
    

    集合无序不重复

  • 相关阅读:
    75. 颜色分类
    排序链表
    两个数组的交集
    242. 有效的字母异位词
    排序优化
    622.设计循环队列
    比较含退格的字符串
    682.棒球比赛
    496.下一个更大的元素I
    线性排序算法
  • 原文地址:https://www.cnblogs.com/Bread-Wang/p/10179076.html
Copyright © 2020-2023  润新知