9.Set类型 - 集合

作者: HassGy | 来源:发表于2019-08-01 14:37 被阅读12次

    set类型就是集合,集合本身时不可hash,即可变。
    ☑特点:元素之间具有不重复性,无序性,用"{}"括住可hash(不可变)的元素(int/str/tuple/bool)
    ☑算术运算:并,交,补集
    ☑两种类型:set普通集合,frozenset不可变集合
    ⚠集合没有索引,也是不可以定位一个元素,所以修改的方法只能先清空后添加。


    内置函数

    👉查询 -集合是一个可迭代对象,可以进行for循环迭代

    x = set(('apple', 'banana', 'cherry'))
    print(x)
    
    for n in x:
        print(n)
    --------------------------------------------------
    {'cherry', 'apple', 'banana'}
    cherry
    apple
    banana
    

    👉增加 add()/update()

    fruits = {'apple', 'banana', 'cherry'}
    fruits.add('orange') 
    print(fruits)
    --------------------------------------------------------
    x = {'apple', 'banana', 'cherry'}
    y = {'google', 'microsoft', 'apple'}
    x.update(y) 
    print(x)
    

    👉修改 -没有直接修改,只能先删除,后添加。

    s = {"刘嘉玲", '关之琳', "王祖贤","张曼⽟", "李若彤"}
    # 把刘嘉玲改成赵本⼭
    s.remove("刘嘉玲")
    s.add("赵本⼭")
    print(s)
    

    👉删除 -pop()/remove()/clear()

    fruits = {'apple', 'banana', 'cherry'}
    fruits.pop() 
    print(fruits)
    ---------------------------------------------------
    fruits = {'apple', 'banana', 'cherry'}
    fruits.remove('banana') 
    print(fruits)
    ---------------------------------------------------
    fruits = {'apple', 'banana', 'cherry'}
    fruits.clear()
    print(fruits)
    

    其他操作函数 (&,|,-,^)都可以取代以下关键字来运算。

    # 交集元素
    x = {'apple', 'banana', 'cherry'}
    y = {'google', 'microsoft', 'apple'}
    z = x.intersection(y)
    print(z)
    
    x = {'a', 'b', 'c'}
    y = {'c', 'd', 'e'}
    z = {'f', 'g', 'c'}
    result = x.intersection(y, z)
    print(result)
    ---------------------------------------------------------------
    # 并集元素
    x = {'apple', 'banana', 'cherry'}
    y = {'google', 'microsoft', 'apple'}
    z = x.union(y) 
    print(z)
    
    x = {'a', 'b', 'c'}
    y = {'f', 'd', 'a'}
    z = {'c', 'd', 'e'}
    result = x.union(y, z) 
    print(result)
    ----------------------------------------------------------------
    # 差集元素
    x = {'apple', 'banana', 'cherry'}
    y = {'google', 'microsoft', 'apple'}
    z = x.difference(y)
    print(z)
    
    x = {'apple', 'banana', 'cherry'}
    y = {'google', 'microsoft', 'apple'}
    z = y.difference(x) 
    print(z)
    ----------------------------------------------------------------
    # 反差集
    x = {'apple', 'banana', 'cherry'}
    y = {'google', 'microsoft', 'apple'}
    z = x.symmetric_difference(y) 
    print(z)
    -----------------------------------------------------------------
    # 子集
    x = {'a', 'b', 'c'}
    y = {'f', 'e', 'd', 'c', 'b'}
    z = x.issubset(y) 
    print(z)
    
    x = {'a', 'b', 'c'}
    y = {'f', 'e', 'd', 'c', 'b', 'a'}
    z = x.issubset(y) 
    print(z)
    ------------------------------------------------------------------
    # 超集
    x = {'f', 'e', 'd', 'c', 'b', 'a'}
    y = {'a', 'b', 'c'}
    z = x.issuperset(y) 
    print(z)
    
    x = {'f', 'e', 'd', 'c', 'b'}
    y = {'a', 'b', 'c'}
    z = x.issuperset(y) 
    print(z)
    

    基本的数据类型已经讲完80%了,接下来会进行一些必要补充。

    相关文章

      网友评论

        本文标题:9.Set类型 - 集合

        本文链接:https://www.haomeiwen.com/subject/gzpqdctx.html