-
看代码写结果
v1 = [1,2,3,4,5] v2 = [v1,v1,v1] v1.append(6) print(v1) print(v2)
v1 = [1,2,3,4,5,6] v2 = [[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]
-
看代码写结果
v1 = [1,2,3,4,5] v2 = [v1,v1,v1] v2[1][0] = 111 v2[2][0] = 222 print(v1) print(v2)
结果:
v1 = [222,2,3,4,5] v2 = [[222,2,3,4,5], [222,2,3,4,5], [222,2,3,4,5]]
-
看代码写结果,并解释每一步的流程。
v1 = [1,2,3,4,5,6,7,8,9] v2 = {} for item in v1: if item < 6: continue if 'k1' in v2: v2['k1'].append(item) else: v2['k1'] = [item ] print(v2)
结果:
v1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] v2 = {} # 建立空字典 for item in v1: # 遍历列表v1 if item < 6: # 如果列表元素小于6时中止继续下次循环 continue if 'k1' in v2: # item大于等于6时,如果'k1'在v2字典中 v2['k1'].append(item) # 'k1'的键值追加item else: v2['k1'] = [item] # 否则直接增加键值对:'k1':item print(v2) # v2 = {'k1':[6,7,8,9}
-
简述深浅拷贝?
-
看代码写结果
import copy v1 = "alex" v2 = copy.copy(v1) v3 = copy.deepcopy(v1) print(v1 is v2) print(v1 is v3)
结果:
True True
-
看代码写结果
import copy v1 = [1,2,3,4,5] v2 = copy.copy(v1) v3 = copy.deepcopy(v1) print(v1 is v2) print(v1 is v3)
False False
-
看代码写结果
import copy v1 = [1,2,3,4,5] v2 = copy.copy(v1) v3 = copy.deepcopy(v1) print(v1[0] is v2[0]) print(v1[0] is v3[0]) print(v2[0] is v3[0])
结果:
True True True
-
看代码写结果
import copy v1 = [1,2,3,4,[11,22]] v2 = copy.copy(v1) v3 = copy.deepcopy(v1) print(v1[-1] is v2[-1]) print(v1[-1] is v3[-1]) print(v2[-1] is v3[-1])
结果:
True False False
-
看代码写结果
import copy v1 = [1,2,3,{"name":'太白',"numbers":[7,77,88]},4,5] v2 = copy.copy(v1) print(v1 is v2) print(v1[0] is v2[0]) print(v1[3] is v2[3]) print(v1[3]['name'] is v2[3]['name']) print(v1[3]['numbers'] is v2[3]['numbers']) print(v1[3]['numbers'][1] is v2[3]['numbers'][1])
结果:
False True True True True True
-
看代码写结果
import copy v1 = [1,2,3,{"name":'太白',"numbers":[7,77,88]},4,5] v2 = copy.deepcopy(v1) print(v1 is v2) print(v1[0] is v2[0]) print(v1[3] is v2[3]) print(v1[3]['name'] is v2[3]['name']) print(v1[3]['numbers'] is v2[3]['numbers']) print(v1[3]['numbers'][1] is v2[3]['numbers'][1])
结果:
False True False True False True