我试图为elements
中的每个元素做一个选择,然后将elements
列表中的元素与其首选项(一、二或三)配对。选择主要是关于元素的概率(weights
)。在此之前的代码:
from numpy.random import choice
elements = ['one', 'two', 'three']
weights = [0.2, 0.3, 0.5]
chosenones= []
for el in elements:
chosenones.append(choice(elements,p=weights))
tuples = list(zip(elements,chosenones))
收益率:
[('one', 'two'), ('two', 'two'), ('three', 'two')]
我需要的是,每个元素都要做出两个而不是一个选择。你知道吗
预期输出应如下所示:
[('one', 'two'), ('one', 'one'), ('two', 'two'),('two', 'three'), ('three', 'two'), ('three', 'one')]
你知道如何得到这个输出吗?你知道吗
如果需要两个值,只需告诉
numpy.random.choice()
选取两个值;在循环时将el
值作为元组包含(无需使用zip()
):或者使用列表理解:
通过设置} documentation :
replace=False
,可以得到唯一的值;删除它或显式地将其设置为True
,以允许重复。参见^{演示:
如果接受重复项,^{} 将执行以下操作:
相关问题 更多 >
编程相关推荐