Python --集合
发布日期:2021-05-10 11:46:01 浏览次数:18 分类:精选文章

本文共 2721 字,大约阅读时间需要 9 分钟。

���������Python���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������

  • ������������������������������������������������������������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������������������������
  • ������������

    ���������������������������{}���������������������������set()���������������������������������

    s = {1, 2, 3, 4, 5}
    s = set([1, 2, 3, 4, 5])
    # ���������������������
    s = {1, 'a', 10}

    ������������������������������������������������������������������������������������������������������������

    # ������������������
    s = { [1, 2, 3], [4, 5, 6] }
    # ���������������TypeError: unhashable type: 'list'

    ���������������������

    ������������������������������������������������������������������������

  • ���������������

    • ���������������������������������������
      print('c' in s)
    • ������������������������������
      print(len(s))
  • ���������������

    • ���������������
      s.add(10)
      s.add(30)
    • ���������������
      s.update([10, 20, 30])
      s.update({'a': 1, 'b': 2})
  • ���������������

    • ������������������������
      s.remove(10)
      s.remove(1000)
    • ���������������������
      popped = s.pop()
  • ���������������

    s.clear()
  • ���������������

    s_copy = s.copy()
  • ���������������

    ���������������������������������

  • ���������������&���������

    s = {1, 2, 3, 4, 5}
    s2 = {3, 4, 5, 6, 7}
    result = s & s2
    # ���������{3, 4, 5}
  • ���������������|���������

    result = s | s2
    # ���������{1, 2, 3, 4, 5, 6, 7}
  • ���������������-���������������������������������������������������������������������������

    result = s - s2
    # ���������{1, 2}
  • ������������������^������������������������������������������������������������

    result = s ^ s2
    # ���������{1, 2, 6, 7}
  • ������������������

    • ������������������������������������������������
      a = {1, 2, 3}
      b = {1, 2, 3, 4, 5}
      result = a <= b
      # ���������True
      result = b <= a
      # ���������False
    • ���������������������������
      result = a < b
      # ���������True
      result = a < a
      # ���������False
  • ���������������

    result = b > a
    # ���������True
  • ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ���������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:Python --函数
    下一篇:Python --字典

    发表评论

    最新留言

    哈哈,博客排版真的漂亮呢~
    [***.90.31.176]2025年04月28日 20时07分17秒