1

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. 
frhyme
  • 966
  • 1
  • 15
  • 24
  • See [Why are slices in Python 3 still copies and not views?](https://stackoverflow.com/questions/6902235/why-are-slices-in-python-3-still-copies-and-not-views) – Peter Wood May 23 '17 at 13:00

2 Answers2

0

That's because you create copies of the original list by lst[:mid], lst[mid:]. A solution is to pass to change() the same list and (separately) the range to process.

Kirill Bulygin
  • 3,658
  • 1
  • 17
  • 23
0

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]
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555