简体   繁体   English

我可以传入一个列表作为 random.choices() 的权重参数吗

[英]Can I pass in a list as the weights parameter for random.choices()

I am trying to use the random.choices method with two lists: names and values.我正在尝试将 random.choices 方法与两个列表一起使用:名称和值。 The values list is the weights.值列表是权重。

I am calling the method by doing我通过做调用方法

random.choices(names, weights = values, k = 1)

The error I am receiving is我收到的错误是

ValueError: The number of weights does not match the population

Can I not use a list as a parameter for the weights?我不能使用列表作为权重的参数吗? If I can pass in a list in for the population (names), why can't I pass in a list for the weights?如果我可以为人口(姓名)传递一个列表,为什么我不能为权重传递一个列表?

Because I was reading in my data using pandas DataFrame functions, I was trying to pass DataFrame type data into random.choices which it did not like.因为我正在使用 pandas DataFrame 函数读取数据,所以我试图将 DataFrame 类型的数据传递到它不喜欢的 random.choices 中。 I converted the data to lists and it worked.我将数据转换为列表并且它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM