|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
切片可以复制字符串,我这样做了,但是对复制的字符串进行for循环删除后,原本的字符串也发生相应的改变,如图所示。百思不得其姐,请各位大神指点一下
1.切片是浅拷贝,内层元素还是引用。推荐阅读: https://blog.csdn.net/xbinworld/article/details/104203829, https://fishc.com.cn/forum.php?m ... =%C7%B3%BF%BD%B1%B4
2.另外python 函数的参数传递:推荐阅读: https://www.runoob.com/python3/python3-function.html
1)不可变类型(strings,tuples,numbers):类似 C++ 的值传递,如整数、字符串、元组。如 fun(a),传递的只是 a 的值,没有影响 a 对象本身。如果在 fun(a) 内部修改 a 的值,则是新生成一个 a 的对象。
2)可变类型(list,dict):类似 C++ 的引用传递,如 列表,字典。如 fun(la),则是将 la 真正的传过去,修改后 fun 外部的 la 也会受影响。
3.程序改成深拷贝:
- import copy
- def printTable(a):
- test = []
- c = copy.deepcopy(a)
- for i in c:
- while len(i) > 1:
- if i[0] < i[-1]:
- del i[0]
- else:
- del i[-1]
- test.append(len(i[0]))
- print(a)
- print(c)
- tab = [['apples', 'oranges', 'cherries', 'banana'],
- ['alice', 'bob', 'carol', 'david'],
- ['dogs', 'cats', 'moose', 'goose']]
- printTable(tab)
复制代码
|
|