mercredi 23 octobre 2019

Getting weighted random values from a list of lists with different list lengths

I need to create a new list that has random values pulled from a list of lists, where the secondary lists may be of different lengths.

Also, I need to take into account that, for example, if one of the secondary lists is larger than the rest, then the probabilities of obtaining a value from said list must be higher than that of the shorter secondary lists. Random values may be selected more than once, meaning I don't have to remove it from the list of lists after being chosen.

I was able to create the list of lists, where each secondary list corresponds to a region and its contents corresponds to client codes randomly generated, so far so good. But, when I use the function random.choice() to create my new list with random values, I get x amount of random lists from the lists available, rather than random values picked from ALL lists.

thislist = []

So I have my blank list and I am ready to populate the list with, in this case, 10 random values from the list of lists named 'codigo_cliente'

for i in range(10): thislist.append(random.choice(codigo_cliente))

Here are the client codes with 30 total clients in this example:

Clients Codes: [['A-336', 'A-437', 'A-720', 'A-233', 'A-499'], ['B-664', 'B-133', 'B-267', 'B-421', 'B-553', 'B-910', 'B-792', 'B-719', 'B-550', 'B-946'], ['C-755', 'C-533', 'C-596', 'C-877', 'C-400', 'C-354', 'C-471', 'C-169', 'C-329', 'C-318', 'C-550', 'C-422', 'C-251', 'C-852', 'C-309']]

I am getting the following output, which is not what I want:

This is the random list of clients selected: [['B-664', 'B-133', 'B-267', 'B-421', 'B-553', 'B-910', 'B-792', 'B-719', 'B-550', 'B-946'], ['A-336', 'A-437', 'A-720', 'A-233', 'A-499'], ['C-755', 'C-533', 'C-596', 'C-877', 'C-400', 'C-354', 'C-471', 'C-169', 'C-329', 'C-318', 'C-550', 'C-422', 'C-251', 'C-852', 'C-309'], ['A-336', 'A-437', 'A-720', 'A-233', 'A-499'], ['B-664', 'B-133', 'B-267', 'B-421', 'B-553', 'B-910', 'B-792', 'B-719', 'B-550', 'B-946'], ['C-755', 'C-533', 'C-596', 'C-877', 'C-400', 'C-354', 'C-471', 'C-169', 'C-329', 'C-318', 'C-550', 'C-422', 'C-251', 'C-852', 'C-309'], ['C-755', 'C-533', 'C-596', 'C-877', 'C-400', 'C-354', 'C-471', 'C-169', 'C-329', 'C-318', 'C-550', 'C-422', 'C-251', 'C-852', 'C-309'], ['C-755', 'C-533', 'C-596', 'C-877', 'C-400', 'C-354', 'C-471', 'C-169', 'C-329', 'C-318', 'C-550', 'C-422', 'C-251', 'C-852', 'C-309'], ['B-664', 'B-133', 'B-267', 'B-421', 'B-553', 'B-910', 'B-792', 'B-719', 'B-550', 'B-946'], ['A-336', 'A-437', 'A-720', 'A-233', 'A-499']]

Instead, I should be getting something like, for example, the following:

thislist = ['A-336', 'B-553', 'C-596', 'B-910', 'C-251', 'C-329', 'B-910', 'A-437', 'B-946', 'C-251']

Notice how there are more values with the "C" prefix from the larger secondary list, than values with the A or B prefixes from the smaller secondary lists.




Aucun commentaire:

Enregistrer un commentaire