jeudi 30 janvier 2020

Random sampling from dictionary list values based on an integar value from another dictionary, with matching keys

I have two dictionaries, one has items with lists to random sample from:

candidates_to_sample_from = {'PCP3': ['member3', 'member12', 'member17'],
 'PCP4': ['member14', 'member15', 'member16']}

The other has matching keys and the number of samples to draw from and ultimately remove from the pool of members:

sample_counts = Counter({'PCP3': 2, 'PCP4': 1})

I tried doing it two ways without the results I expected. One way is this:

for prov, lst in candidates_to_sample_from.items():
    for i, j in sample_counts.items():
        candidates_out = random.sample(lst, j)

print(candidates_out)
['member16']

This produces only the 1 chosen person from the second item in candidate_to_sample_from but not the two who should be chose from the first item.

Then I also tried creating an empty list and appending to it:

candidates_out = []

for prov, lst in candidates_to_sample_from_dict.items():
    for i, j in sample_counts.items():
        candidates_out.append(random.sample(lst, j))

print(candidates_out)
[['member3', 'member12'], ['members17'], ['member16', 'member14'], ['member16']]

I'm trying to just get a simple list like:

candidates_out = ['member3', 'member17', 'member15']

This has two from item one of candidates_to_sample_from and one from the second. This is a smaller model, it should work the same no matter what the size and scope of the dictionaries




Aucun commentaire:

Enregistrer un commentaire