我正在尝试为布尔值列表随机分配一个“真”值。当我运行代码时,我不断收到错误消息。这是代码:
for x in population:
if x:
r = random.randint(0, len(population))
population[r] = True
Run Code Online (Sandbox Code Playgroud)
不断抛出错误:
"Traceback (most recent call last):
population[r] = True
IndexError: list assignment index out of range"
Run Code Online (Sandbox Code Playgroud)
我确定这是微不足道的事情,但我无法弄清楚。当我将其限制在列表长度内时,索引分配如何超出范围?
python ×1