标签:迭代 语法 创建 set集合 随机 一个 class 操作 报错
j_c0 = {1, 2, 3, 4, 6}
j_c1 = {i for i in j_c0}
print(j_c1)
j_c2 = {i for i in j_c0 if i % 2 == 0}
print(j_c2)
j_c3 = {i**2 for i in j_c0}
print(j_c3)
j_z0 = {1, 2, 3, 4, 5}
j_z0.add(10)
print(j_z0)
print(id(j_z0))
j_z0.update({13, 14})
print(j_z0)
print(id(j_z0))
- .clear(),.remove(),.discard()
- .remove() 删除单个元素时,如果元素不存在,会报错。
- .discard() 删除单个元素时,如果元素不存在,不会报错
- .pop() 随机删除一个元素,返回值是删除的元素
j_m0 = {1, 2, 3, 4}
j_m1 = {3, 4, 5, 6}
j_m2 = {3, 4, 5, 6}
print(id(j_m0))
print(id(j_m0.intersection(j_m1, j_m2)))
print(j_m0.intersection(j_m1, j_m2))
j_m0 = {1, 2, 3, 4}
j_m1 = {3, 4, 5, 6}
j_m2 = {3, 4, 5, 6}
print(id(j_m0))
j_m0.intersection_update(j_m1, j_m2)
print(j_m0)
print(id(j_m0))
j_m0 = {1, 2, 3, 4}
j_m1 = {3, 4, 5, 6}
print(j_m0 - j_m1)
j_m0 = {1, 2, 3, 4}
j_m1 = {3, 4, 5, 6}
print(id(j_m0))
print(j_m0.union(j_m1))
j_m0 = {1, 2, 3, 4}
print(j_m0)
print(frozenset(j_m0))
p = {1:"we", 2:45}
print(str(p))
标签:迭代 语法 创建 set集合 随机 一个 class 操作 报错
原文地址:https://www.cnblogs.com/TK-tank/p/12345456.html