Reputation: 347
It is important to let the readers know that I sometimes have to delete items in my list of lists before calling the function.
Traceback (most recent call last):
File "C:\Users\User\Desktop\permdebunk.py", line 195, in <module>
shuff(c,len(c))
File "C:\Users\User\Desktop\permdebunk.py", line 181, in shuff
c[i],c[j] = c[j],c[i]
IndexError: list index out of range
Python's random.shuffle()
is insufficient for my needs if it will not perform all possible permutations. So I did some google searching and found this function that shuffles a list that supposedly circumvents the problems stated above about Python's random.shuffle()
def shuff(c, n):
for i in range(n-1,0,-1):
random.seed()
j = random.randint(0,i+1)
c[i],c[j] = c[j],c[i]
The problem seems to occur after I append c[0]
to c
and then del c[0]
to push into the list. I have over 200+ lines of code as an experiment into an NP hard problem.Please look at the first log in the link. And remember to check line 195 and 181
Edit: The link didn't have line numbers so use ctrl + f to find the word shuff
What ways can I "push" into the list without getting any index errors? And, why does python throw that error?
Upvotes: 0
Views: 218
Reputation: 4418
random.randint()
includes both endpoints. So when i = n - 1
, randint(0, i + 1)
could return (n - 1) + 1
, which is n
. c[n]
is out of range of the list.
I should think changing the randint()
call to randint(0, i)
should work.
Upvotes: 3