python - 在 Python3 中将列表作为参数传递是如何工作的?
问题描述
当我遇到这个问题时,我试图使用递归对冒泡排序进行编码。
def bubble_sort_recur(a):
print(a)
if len(a)==1:
return
for i in range(len(a)-1):
if a[i] > a[i+1] :
a[i]=a[i]^a[i+1] # swap using XOR
a[i+1]=a[i]^a[i+1]
a[i]=a[i]^a[i+1]
print(a) # shows the changes made in the current call
bubble_sort_recur(a[:-1])
b=[1,6,1,66,3,32,21,33,1] # initial list
bubble_sort_recur(b) # function call
我认为python使用对象(列表)的对象引用传递。递归的输出也是预期的,如下所示:
[1, 6, 1, 66, 3, 32, 21, 33, 1]
[1, 1, 6, 3, 32, 21, 33, 1, 66] # 66 bubbles up
[1, 1, 6, 3, 32, 21, 33, 1]
[1, 1, 3, 6, 21, 32, 1, 33]
[1, 1, 3, 6, 21, 32, 1]
[1, 1, 3, 6, 21, 1, 32]
[1, 1, 3, 6, 21, 1]
[1, 1, 3, 6, 1, 21]
[1, 1, 3, 6, 1]
[1, 1, 3, 1, 6]
[1, 1, 3, 1]
[1, 1, 1, 3]
[1, 1, 1]
[1, 1, 1]
[1, 1]
[1, 1]
[1]
可以看到,第三个“1”如预期的那样冒泡到第二个指数。但是当我这样做时,print(b)
我得到[1, 1, 6, 3, 32, 21, 33, 1, 66]
的既不是排序列表也不是原始列表(b=[1,6,1,66,3,32,21,33,1]
)。您能否解释一下出现这种混合行为的原因?我试图探索的工作如下:
def func(a):
a[0]='only one altered'
def func2(b):
a=['this','is', 'completely', 'new']
a=[1,'initial',2, 'array']
func(a) # this changes the list
print(a)
func2(a) # this does not change the list 'a' , in any way.
print(a)
>> ['only one altered', 'initial', 2, 'array']
['only one altered', 'initial', 2, 'array']
我注意到,当我使用索引更改函数内部列表的值时,实际列表会发生变化。否则列表不受影响。任何帮助表示赞赏。
解决方案
您的函数不返回值,并且依赖于可变列表才能对其进行更改。但是,然后您使用当前列表的一部分调用递归,这会生成一个副本(因此是一个新列表),因此b
不再引用该列表。
print(b)
您可以通过在递归之前添加一行来验证这一点。
def bubble_sort_recur(a):
print(a)
if len(a)==1:
return
for i in range(len(a)-1):
if a[i] > a[i+1] :
a[i]=a[i]^a[i+1] # swap using XOR
a[i+1]=a[i]^a[i+1]
a[i]=a[i]^a[i+1]
print(a) # shows the changes made in the current call
print(b)
print()
bubble_sort_recur(a[:-1])
b=[1,6,1,66,3,32,21,33,1] # initial list
bubble_sort_recur(b) # function call
输出
[1, 6, 1, 66, 3, 32, 21, 33, 1]
[1, 1, 6, 3, 32, 21, 33, 1, 66]
[1, 1, 6, 3, 32, 21, 33, 1, 66] # first call changed b
[1, 1, 6, 3, 32, 21, 33, 1]
[1, 1, 3, 6, 21, 32, 1, 33]
[1, 1, 6, 3, 32, 21, 33, 1, 66] # none of the recursions changed b
[1, 1, 3, 6, 21, 32, 1]
[1, 1, 3, 6, 21, 1, 32]
[1, 1, 6, 3, 32, 21, 33, 1, 66]
[1, 1, 3, 6, 21, 1]
[1, 1, 3, 6, 1, 21]
[1, 1, 6, 3, 32, 21, 33, 1, 66]
[1, 1, 3, 6, 1]
[1, 1, 3, 1, 6]
[1, 1, 6, 3, 32, 21, 33, 1, 66]
[1, 1, 3, 1]
[1, 1, 1, 3]
[1, 1, 6, 3, 32, 21, 33, 1, 66]
[1, 1, 1]
[1, 1, 1]
[1, 1, 6, 3, 32, 21, 33, 1, 66]
[1, 1]
[1, 1]
[1, 1, 6, 3, 32, 21, 33, 1, 66]
[1]
一种解决方案是包括回电。
def bubble_sort_recur(a):
if len(a) > 1:
for i in range(len(a)-1):
if a[i] > a[i+1] :
a[i], a[i+1] = a[i+1], a[i] # more pythonic syntax
# or
# a[i:i+2] = a[i+1:i-1:-1]
# or your original code
# a[i]=a[i]^a[i+1] # swap using XOR
# a[i+1]=a[i]^a[i+1]
# a[i]=a[i]^a[i+1]
print(a)
a[:-1] = bubble_sort_recur(a[:-1]) # catch the result of recursion
return a
b = [1,6,1,66,3,32,21,33,1] # initial list
b = bubble_sort_recur(b) # function call
[1, 1, 6, 3, 32, 21, 33, 1, 66]
[1, 1, 3, 6, 21, 32, 1, 33]
[1, 1, 3, 6, 21, 1, 32]
[1, 1, 3, 6, 1, 21]
[1, 1, 3, 1, 6]
[1, 1, 1, 3]
[1, 1, 1]
[1, 1]
>>> print(b)
[1, 1, 1, 3, 6, 21, 32, 33, 66]
推荐阅读
- microsoft-graph-api - Microsoft Graph API 创建包含自定义元数据列的文件夹
- javascript - 新反应无法启动服务器收到错误消息知道吗?
- vue.js - 如何在 vuejs 中重新挂载子组件
- xamarin.ios - 如何在 xamarin.forms iOS 中配置 WkWebView?
- python - Python randint 生成具有相同位数的数字
- vba - 变体和 if 语句 - VBA
- java - 提交从数据库中提供数据的搜索表单后如何解析 Html
- flutter - Dart 中的日期格式,包括 GMT
- javascript - 如何使用 d3.js 在画布中移动元素时减少延迟
- java - 如何以最优雅的方式找到具有最大价值的对象?