等峰也等你

博客园 首页 新随笔 联系 订阅 管理
  308 随笔 :: 0 文章 :: 0 评论 :: 14975 阅读

集合操作

添加操作

  • add(ele) 向集合中添加一个元素,如果元素则不产生任何影响

    s = {1, 2, 3}

    s.add(4)
    print(s)
    s.add("Hello")
    s.add("Hello")
    print(s)

  • pdate(others) 更新集合,添加来自 others 中的所有元素,others是一个可迭代对象,如果数据在集合中存在则不更新。

    s = {1, 2, 3}

    s.update((4,5,6))
    print(s)
    s.update([5,6,7])
    print(s)
    s.update({6,7,8,9})
    print(s)

删除操作

  • pop() 从集合中移除并返回任意一个元素,如果集合为空,则抛出错误

    s = {1, 2, 3}

    print(s.pop())
    print(s)
    print(s.pop())
    print(s)
    print(s.pop())
    print(s)
    print(s.pop())

  • remove(elem)从集合中移除元素 elem。 如果 elem 不存在于集合中则抛出错误。

    s = {1, 2, 3}

    print(s.remove(1))
    print(s)
    print(s.remove(3))
    print(s)
    print(s.remove(5))

  • discard(elem) 如果元素 elem 存在于集合中则将其移除,如果elem不存在,则什么也不做

    s = {1, 2, 3}

    s.discard(1)
    print(s)
    s.discard(3)
    print(s)
    s.discard(5)

  • clear() 清空集合

    s = {1, 2, 3}
    s.clear()
    print(s)

posted on   等峰也等你  阅读(5)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示