python 面试疑问
作者:互联网
第一期 面试题
from copy import deepcopy
a = [1, [2,3,4],5]
b = a.copy()
# b[0] = 0 # 输出 [1, [2, 3, 4], 5],不会影响a的结果
# b[1][0] = 1 # 输出 1, [1, 3, 4], 5],a的结果也发生的改变,copy函数不能嵌套拷贝,需要使用deepcopy函数才行。
# b[1][0] = [] # 输出 [1, [[], 3, 4], 5],a的结果也发生改变
# b[1][0:1] = 1 # 报错 TypeError: can only assign an iterable,b[1][0:1]返回是一个list类型,不能使用1 int类型给它赋值
# b[1][0:1] = [] # 输出 [1, [3, 4], 5]
# b[1][1:2] = [] # 输出 [1, [2, 4], 5]
# b[1][0:2] = [] # 输出 [1, [4], 5]
# b[1][0:] = [] # 输出 [1, [], 5]
b[1] = [1,2] # 输出 [1, [2, 3, 4], 5]
# b[1][:] = [1,2] # 输出 [1, [1, 2], 5]
# b[1][0:1] = [1,2] # 输出 [1, [1, 2, 3, 4], 5]
print(a)
记录某次 python 的面试题,全程懵逼,python索引切片的原理,求个大佬指点下啊。
标签:输出,面试题,结果,python,面试,deepcopy,copy,疑问 来源: https://www.cnblogs.com/hypnus-ly/p/15291610.html