五、集合
无序
无重复
-
独有功能
-
add添加
v = {1,6}
v.add('sss')
v.add -
discard:删除
v = {1,6}
v.discard(1)
print(v) -
update:批量添加
v = {1,2}
v.update({55,66})
print(v) -
intersection:交集
v = {1,2,3}
v1 = v2.intersection({1,2})
print(v1) -
union:并集
v = {1,2,3}
v1 = v2.union({1,2})
print(v1) -
difference:差集
v = {1,2,3}
v1 = v2.intersection({1,2})#v中有且{1,2}中没有
print(v1)注意:intersection({})中可以是{}也可以是[].但头部不行
-
-
公共功能
-
len
v = {1,2}
print(len(v)) -
for循环
v = {1,2,3}
for item in v
print(item) -
集合的嵌套:列表/字典/集合/——>不能放在集合中+不能作为字典的key(unhashable
-
-
内存相关
-
示例一
v1 = 555
v2 = 555 #内存地址不相等
-
#按理v1 和 v2应该是不同的内存地址,特殊: 1、整形: -5 ~256 2、字符串:
2. 示例二
```python
v1 = 555
v2 = v1
-
查询内存地址
print(id(v1))
-
问题:== 和 is有什么区别
-
==是值是否相等
-
-
-

浙公网安备 33010602011771号