【发布时间】:2019-04-24 23:05:16
【问题描述】:
我正在编写一个遗传算法,并希望随机改变列表中的一个数字。
如果我有一个列表,例如:
[1,2,3,4,5]
我如何获取列表并将其中一个数字更改为随机数,例如:
[1,2,7,4,5]
或
[1,3,3,4,5]
【问题讨论】:
-
你试过什么?只需随机选择一个索引并在循环中随机选择一个值
-
list[random.randint(0,len(list)] = random.randint(0,maxtobedefine) docs.scipy.org/doc/numpy-1.15.1/reference/generated/…
-
@Alexis 这些是 numpy 的文档。这里不需要使用 numpy 来生成随机数,除非 OP 实际上有一个数组
-
@Alexis:
IndexError -
另外,将变量命名为
list也不是一个好主意
标签: python list random genetic-algorithm