简体   繁体   English

在递归调用python中交换列表元素

[英]swap elements of list in recursive call python

I want to make simple function swap random element in list. 我想在列表中创建简单的函数交换随机元素。 but it doesn't work in recursive call. 但它在递归调用中不起作用。

in first recursive call, element swapping work, but nested recursive call(or nested recursive call in first recursive call) doesn't work. 在第一次递归调用中,元素交换工作,但嵌套递归调用(或第一次递归调用中的嵌套递归调用)不起作用。

I don't know why only swap in first recursive call works. 我不知道为什么只交换第一次递归调用工作。

below are result. 以下是结果。

Thank you all. 谢谢你们。

def change(lst):
    if len(lst)>4:
        a, b = np.random.randint(0, len(lst)), np.random.randint(0, len(lst))
        print(lst)
        lst[a], lst[b] = lst[b], lst[a]
        print(lst)
        mid = int(len(lst)/2)
        change(lst[:mid])
        change(lst[mid:])
k = list(range(0, 20))
change(k)
print(k)

` `

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[0, 19, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 1]
[0, 19, 2, 3, 4, 5, 6, 7, 8, 9]
[3, 19, 2, 0, 4, 5, 6, 7, 8, 9]
[3, 19, 2, 0, 4]
[3, 0, 2, 19, 4]
[5, 6, 7, 8, 9]
[5, 6, 8, 7, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 1]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 1]
[10, 11, 12, 13, 14]
[10, 14, 12, 13, 11]
[15, 16, 17, 18, 1]
[15, 16, 17, 18, 1]
[0, 19, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 1] <= result. 

That's because you create copies of the original list by lst[:mid] , lst[mid:] . 那是因为你通过lst[:mid]lst[mid:]创建原始列表的副本 A solution is to pass to change() the same list and (separately) the range to process. 解决方案是传递给change()相同的列表和(单独)要处理的范围。

The problem is that in your recursive calls: 问题在于你的递归调用:

change(lst[:mid])
change(lst[mid:])

you use a slicing operator . 你使用切片操作符 The slicing operator constructs a new list , so your changes are made on a new list and are not reflected on the original list (since it is a copy ). 切片运算符构造一个新列表 ,因此您的更改将在新列表中进行 ,并且不会反映在原始列表中(因为它是一个副本 )。

What you can do is use indices instead: 您可以做的是使用索引:

def change(lst,frm=0,to=None):
    if to is None: # set the default to the end of the list to = len(lst)
    if to-frm > 4:
        a, b = np.random.randint(frm,to), np.random.randint(frm,to)
        print(lst)
        lst[a], lst[b] = lst[b], lst[a]
        print(lst)
        mid = (frm+to)//2
        change(lst,frm,mid)
        change(lst,mid,to)

Then we obtain: 然后我们获得:

>>> k = list(range(0, 20))
>>> change(k)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
>>> print(k)
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM