Reputation: 3
I've looked at a few threads here and for I haven't been able to digest it completely or make my code work. It's pretty simple but I've been having a lot of problems with this so here it is:
I have a list
lst = ["a", "b", "c", "d"]
And I'm trying to swap two elements by referencing their placement on the list, so far I have,
edit I'm not allowed use the in place swap in Python
def swap(i, j, lst):
placehold_swap = j
j = i
i = placehold_swap
I've been thinking about doing something like this in order to make update the order of the list
lst = lst
Obviously this doesn't work. My first instinct was to do
return lst
But in the assignment I'm not allowed to use return or print, I'm only allowed to modify lst
The next step is to create another function inside a loop that shuffles the list by swapping random elements from the list 100+ times
import random
def shuffle(lst):
for i in range (100):
placehold_shuffle = []
choose1 = random.randint(0, len(lst)-1)
choose2 = random.randint(0, len(lst)-1)
placehold_shuffle = swap(choose1, choose2, lst)
I'm not allowed to have a return lst or any type of return so think I should have a list = [something] somewhere inside or outside the loop so that once something is shuffled it saves it as the updated list and then shuffles it again inside the loop but I'm not sure how to do that and I haven't gotten to play around with it since I can't get my first function to work.
To follow up*
I got my two functions working like this:
def swap(i, j, lst):
placehold_swap = lst[j]
lst[j] = lst[i]
lst[i] = placehold_swap
import random
def shuffle(lst):
for i in range (100):
choice1 = random.randint(0, len(lst)-1)
choice2 = random.randint(0, len(lst)-1)
swap(choice1, choice2 , lst)
Upvotes: 0
Views: 86
Reputation: 1576
You can do an inplace swap of values in Python, so you do not need to save the value you are overwriting first when swapping.
def swap(x, y, li):
li[x], li[y] = li[y], li[x]
A handy function from the random standard library is choice. You can let choice choose a random index from a list with all indices, which can be constructed by using the range function based on the length of the list.
from random import choice
def shuffle(li):
for i in range(100):
swap(choice(range(len(li))), choice(range(len(li))), li)
Say your list is 4 elements long, then range(4) is a sequence with elements (0, 1, 2, 3). The choice function picks one for you and you can directly pass that as argument to your swap function, together with your list to make sure it is in scope.
In Python you can even shorten the code further by putting the loop inside a list comprehension. But that is optional and in this particular situation probably a choice of preference.
def shuffle(li):
[swap(choice(range(len(li))), choice(range(len(li))), li) for i in range(100)]
Upvotes: 0
Reputation: 104712
Your swap
function does nothing because you're not interacting with lst
at all. You need to replace the i
and j
values you're using in the assignments with lst[i]
and lst[j]
. With that change your shuffle should work (though you don't need placehold_shuffle
since swap
doesn't return anything).
Note that swapping values in Python can actually be done more easily than in most other languages, thanks to tuple packing and unpacking:
def swap(i, j, lst):
lst[i], lst[j] = lst[j], lst[i]
Don't worry to much if you don't understand this yet. Your current method of swapping can work fine too as long as you're actually changing elements of the list, rather than just the variables i
and j
.
Upvotes: 3