美文网首页
集合的相关操作

集合的相关操作

作者: young后知后觉 | 来源:发表于2023-09-22 20:56 被阅读0次

    集合元素的判断操作:

    in 或者 not in 

    a={1,2,3,4,5,"我","python","你好"}

    print(1 in a)                       True

    print(1 not in a)                 False

    print("我" in a )                 True

    print("我们" not in a )      True

    增删改查

    discard( )  (抛弃,丢弃) 一次删除一个指定元素

    a={1,2,3,4,5,"我","python","你好"}

    a.discard(1)

    print(a)

    {2,3,4,5,"我","python","你好"}

    remove( ) (去除,去掉)  一次删除一个指定元素

    a={1,2,3,4,5,"我","python","你好"}

    a.remove(5)

    print(a)

    {1,2,3,4,"我","python","你好"}

    pop( ) 后面不能放参数,放参数就会报错,一次只删除一个任意元素,删除元素是随机的

    a={1,2,3,4,5,"我","python","你好"}

    a.pop( )

    print(a)

    {1,2,3,4,"我","python","你好"} 也有可能   a={1,2,3,4,5,"我","python"}  随机删除一个元素

    add( ) 添加  一次添加一个元素 ,添加成功后的元素位置是随机的

    a={1,2,3,4,5,"我","python","你好"}

    a.add("我们")

    print(a)

    {1,2,3,4,5,"我","我们","python","你好"}

    update( )     更新   至少添加一个元素

    注意:a.update( {“字符串”} ) 将字符串添加到集合中,有重复的会忽略。

                a.update( “字符串” ) 将字符串拆分单个字符后,然后再一个个添加到集合中,有重复的会忽略。

    a={1,2,3,4,5,"我","python","你好",6,7,8,9}

    a.update([10,11,"我们"])

    print(a)

    {1,2,3,4,5,"我","python","你好",6,7,8,9,10,11,"我们"}

    a={1,2,3,4,5,"我","python","你好",6,7,8,9}

    a.update(["我们"])

    print(a)

    {1,2,3,4,5,"我","python","你好",6,7,8,9,"我","们"}

    clear( )  清空集合

    a={1,2,3,4,5,"我","python","你好",6,7,8,9}

    a.clear( )

    print(a)

    set( )

    71.集合的相关操作_哔哩哔哩_bilibili    9.23

    相关文章

      网友评论

          本文标题:集合的相关操作

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