因此,我在拆分列表之前先对列表进行改组。这是从文件中读取的数字列表,因此该列表实际上是字符串列表,但它们是数字:
streams = ['0','1','2','3','4','5','6','7','8','9']
现在,我运行的代码只是对列表进行随机排序:
print(streams, type(streams[0]))
import random
random.shuffle(streams)
我正在使用Python 3.6.4。这是我得到的错误:
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
这对我说,它不能混洗由字符串格式的数字组成的列表?那正确吗?那是错误还是我做错了?
答案 0 :(得分:1)
我认为我已经设法复制了您的错误,将您的信息流更改为列表。
streams_old = "0,1,2,3,4,5,6,7,8,9"
streams = ['0','1','2','3','4','5','6','7','8','9']
print(streams, type(streams[0]))
import random
random.shuffle(streams)
答案 1 :(得分:1)
另一种方式可能是:
streams = "0,1,2,3,4,5,6,7,8,9"
new_streams = streams.split(',')
import random
random.shuffle(new_streams)
print(new_streams)