def bubbleSort(aList): for passnum in range(len(aList)-1,-1): for i in range(passnum): if aList[i]>aList[i+1]: temp = aList[i] aList[i] = aList[i+1] aList[i+1] = temp return aLista=[3,2,1]b=aa=bubbleSort(a)print(a)print(b)
输出:
[1,3][1,3]a=[3,1]b=aa=[1,3]print(a)print(b)
输出:
[1,3][3,1]解决方法
a=[3,1]b=a # **here you're refrencing by memory not value**a=bubbleSort(a)print ID(a) print ID(b)# these both will give you same memory referenceprint(a)print(b)
在第二个例子中,你正在做b = a你正在通过内存引用,但当你做了= [1,3]你将a关联到一个新的内存引用b仍然绑定到旧的内存.
a = [3,1]b=aprint ID(b) #4376879184print ID(a) #4376879184#they will be having the same IDa = [1,3]#Now you have assigned a new address which will have the new arrayprint ID(b) #4376879184print ID(a) #4377341464#they will be having different ID Now总结
以上是内存溢出为你收集整理的在python中分配,被视为对象的副本全部内容,希望文章能够帮你解决在python中分配,被视为对象的副本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)