简体   繁体   中英

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. 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. I converted the data to lists and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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