我想从两个项目生成一个长度为10000的列表('是','否')。我拥有的代码可以做到这一点。问题是,它会产生〜50%是和50%否。如何修改此代码,以便可以设置选择“是”的时间百分比。假设我希望有36.7%的时间是。然后,应选择剩余的63.3%剩余时间“否”。代码如下:
import random
category = ('yes','no')
length_of_field = 10000
print(length_of_field)
print(type(category))
category_attribute = [random.choice(category) for _ in range(length_of_field)]
print('\ncategory:')
print(len(category_attribute))
print(type(category_attribute))
from collections import Counter
a= Counter(category_attribute).keys()
b= Counter(category_attribute).values()
print(a,b)
import numpy as np
alist = np.random.choice(["No","Yes"], 1000, p=[0.633, 0.367])
内置
import random
alist = random.choices(["no", "yes"], weights=[0.633, 0.367], k=1000)