五、集合

set() :空集合

无序

无重复

  1. 独有功能

    1. add添加

      v = {1,6}
      v.add('sss')
      v.add
    2. discard:删除

      v = {1,6}
      v.discard(1)
      print(v)
    3. update:批量添加

      v = {1,2}
      v.update({55,66})
      print(v)
    4. intersection:交集

      v = {1,2,3}
      v1 = v2.intersection({1,2}
      print(v1)
    5. union:并集

      v = {1,2,3}
      v1 = v2.union({1,2}
      print(v1)
    6. difference:差集

      v = {1,2,3}
      v1 = v2.intersection({1,2}#v中有且{1,2}中没有
      print(v1)

      注意:intersection({})中可以是{}也可以是[].但头部不行

  2. 公共功能

    1. len

      v = {1,2}
      print(len(v))
    2. for循环

      v = {1,2,3}
      for item in v
      print(item)
    3. 集合的嵌套:列表/字典/集合/——>不能放在集合中+不能作为字典的key(unhashable

  3. 内存相关

    1. 示例一

    v1 = 555
    v2 = 555 #内存地址不相等

#按理v1 和 v2应该是不同的内存地址,特殊: 1、整形: -5 ~256 2、字符串:

   
2. 示例二

```python
v1 = 555
v2 = v1
  1. 查询内存地址

    print(id(v1))

  2. 问题:== 和 is有什么区别

    1. ==是值是否相等

    2. is 是比较内存是否相等

    3.  

posted @ 2019-09-30 10:23  大大的西红柿  阅读(112)  评论(0)    收藏  举报