Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get all combinations from a random sample list

I am having an issue trying to get all of the combinations of two random sample lists.

I have two lists, one contains all 10k golfers and the other contains all 7k golfers. What I want to do is get all the lineup combinations of golfers (6 golfers per lineup) where I include 2 golfers from the 10k range and 4 golfers from the 7k range.

Currently the list that contains the combinations only includes 6 combinations and I am assuming it’s because of a mistake is in the for loop but I am not sure what to set the range as in order to get all combinations:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

tenKRange = []
sevenKRange = []
lineup = []

with open('combinations.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        if int(row[1]) >= 10000:
            tenKRange.append(row)
        ...
        elif int(row[1]) < 8000 and int(row[1]) >= 7000:
            sevenKRange.append(row)
  

for n in range(len(combinations(random.sample(tenKRange,2) + (random.sample(sevenKRange,4)), 6))):
    lineup.append(list(combinations(random.sample(tenKRange,2) + (random.sample(sevenKRange,4)), 6)))

>Solution :

IIUC, you want to find all possible ways to pick 2 golfers out of 10k, and 4 out of 7k, and then get the product of these two sets? If this is the case, you need to do it in those steps:

import itertools 

sel_10k = itertools.combinations(tenKRange, 2)
sel_7k = itertools.combinations(sevenKRange, 4)

lineup = [p + q for p, q in itertools.product(sel_10k, sel_7k)]

With example lists that contain fewer elements:

tenKRange = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sevenKRange = [101, 102, 103, 104, 105, 106, 107]

this gives the expected lineups:

[(1, 2, 101, 102, 103, 104),
 (1, 2, 101, 102, 103, 105),
 (1, 2, 101, 102, 103, 106),
 (1, 2, 101, 102, 103, 107),
 (1, 2, 101, 102, 104, 105),
 ...
 (2, 8, 101, 103, 104, 105),
 (2, 8, 101, 103, 104, 106),
 (2, 8, 101, 103, 104, 107),
 (2, 8, 101, 103, 105, 106),
 (2, 8, 101, 103, 105, 107),
 ...
 (9, 10, 103, 104, 105, 106),
 (9, 10, 103, 104, 105, 107),
 (9, 10, 103, 104, 106, 107),
 (9, 10, 103, 105, 106, 107),
 (9, 10, 104, 105, 106, 107)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading