今日内容
1.集合
2内存相关
1.集合set
- 无序
- 无重复(可以去重)
v = {1,2,3,4,5,6,99,100} # 疑问:v = {} """ None int v1 = 123 v1 = int() --> 0 bool v2 = True/False v2 = bool() -> False str v3 = "" v3 = str() list v4 = [] v4 = list() tuple v5 = () v5 = tuple() dict v6 = {} v6 = dict() set v7 = set() """
- 集合独有功能
-
-
# v = {1,2} # v.add('Lishaoqi') # v.add('Lishaoqi') # print(v)
-
discard
# v = {1,2,'李邵奇'} # v.discard('李邵奇') # print(v)
-
update
# v = {1,2,'李邵奇'} # v.update({11,22,33}) # print(v)
-
intersection
# v = {1,2,'李邵奇'} # result = v.intersection({1,'李邵奇','小黑'}) # print(result)
-
union
# v = {1,2,'李邵奇'} # result = v.union({1,'李邵奇','小黑'}) # print(result)
-
difference
# v = {1,2,'李邵奇'} # result = v.difference({1,'李邵奇','小黑'}) # v中有且{1,'李邵奇','小黑'}没有 # print(result)
# v1 = {1,'李邵奇','小黑'} # result1 = v1.difference({1,2,'李邵奇'}) # print(result1)
-
symmetric_difference
# v = {1,2,'李邵奇'} # result = v.symmetric_difference({1,'李邵奇','小黑'}) # print(result)
-
- 公共功能
-
len
v = {1,2,'李邵奇'} print(len(v))
- for循环
# v = {1,2,'李邵奇'} # for item in v: # print(item)
- 索引【无】
- 步长【无】
- 切片【无】
- 删除【无】pop,随机删除
- 修改【无】不能删除
-
- 嵌套问题
# 1. 列表/字典/集合 -> 不能放在集合中+不能作为字典的key(unhashable) # info = {1, 2, 3, 4, True, "国风", None, (1, 2, 3)} # print(info) # 2. hash -> 哈希是怎么回事? # 因为在内部会将值进行哈希算法并得到一个数值(对应内存地址),以后用于快速查找。 # 3. 特殊情况 # info = {0, 2, 3, 4, False, "国风", None, (1, 2, 3)} # print(info) # info = { # 1:'alex', # True:'oldboy' # } # print(info)
2.内存相关
示例一
v1 = [11,22,33] v2 = [11,22,33] v1 = 666 v2 = 666 v1 = "asdf" v2 = "asdf" # 按理 v1 和 v2 应该是不同的内存地址。特殊: 1. 整型: -5 ~ 256 2. 字符串:"alex",'asfasd asdf asdf d_asdf ' ----"f_*" * 3 - 重新开辟内存。
示例二
v1 = [11,22,33,44]
v1 = [11,22,33]
示例三
v1 = [11,22,33] v2 = v1 # 练习1 (内部修改) v1 = [11,22,33] v2 = v1 v1.append(666) print(v2) # 含 666 # 练习2:(赋值) v1 = [11,22,33] v2 = v1 v1 = [1,2,3,4] print(v2) # 练习3:(重新赋值) v1 = 'alex' v2 = v1 v1 = 'oldboy' print(v2)
示例四
v = [1,2,3] values = [11,22,v] # 练习1: """ v.append(9) print(values) # [11,22,[1,2,3,9]] """ # 练习2: """ values[2].append(999) print(v) # [1, 2, 3, 999] """ # 练习3: """ v = 999 print(values) # [11, 22, [1, 2, 3]] """ # 练习4: values[2] = 666 print(v) # [1, 2, 3]
示例五
v1 = [1,2] v2 = [2,3] v3 = [11,22,v1,v2,v1]
查看内存地址
""" v1 = [1,2,3] v2 = v1 v1.append(999) print(v1,v2) print(id(v1),id(v2)) """ """ v1 = [1,2,3] v2 = v1 print(id(v1),id(v2)) v1 = 999 print(id(v1),id(v2)) """
-
== 用于比较值是否相等。
-
is 用于比较内存地址是否相等。