vendredi 27 juillet 2018

Am I using random.shuffle wrong or is this a bug?

So I'm shuffling a list before I split it. it's a list of numbers read from a file so the list is actually a list of strings, but they're numbers:

streams = ['0','1','2','3','4','5','6','7','8','9']

now, the code I run, simply shuffles the list:

print(streams, type(streams[0]))
import random
random.shuffle(streams)

I'm using Python 3.6.4 by the way. Here's the error I get:

C:\src>python cli.py
0,1,2,3,4,5,6,7,8,9
 <class 'str'>
Traceback (most recent call last):
...
  File "....py", line 3, in split_randomized_list
    random.shuffle(streams)
  File "C:\ProgramData\Anaconda3\lib\random.py", line 275, in shuffle
    x[i], x[j] = x[j], x[i]
TypeError: 'str' object does not support item assignment

This says to me that it can't shuffle lists made up of numbers in string format? Is that right? Is that a bug or am I doing it wrong?




Aucun commentaire:

Enregistrer un commentaire