Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create all possible unique lists

I am writing a short program for my basketball team. I have gotten Coach to divide the players into lists that correspond to a specific position. (List1 = Point Guards)

Using these lists, I want to create an output with all possible "valid" lineups.

Currently, I have written a basic program that selects 5 unique people from each list

How can I cause this to loop in such a way that all "Valid" configurations of 5 players are printed out?

Any suggestions or direction is greatly appreciated!

Here is what I have so far:

import sys
import random    

list1 = ['Gabe', 'taylor', 'kyle', 'jay']
list2 = ['Gabe', 'Taylor', 'Kyle', 'Jay', 'James', 'John', 'Tyde','Chris', 'Bruno', 'David']
list3 = ['Gabe', 'Taylor', 'Kyle', 'Jay', 'James', 'John', 'Tyde','Chris', 'Bruno', 'David']
list4 = ['Kyle', 'James', 'John', 'Tyde','Bruno', 'Drew', 'Chris']
list5 = ['James', 'John', 'Brendan','Tim', 'Drew' ]
FinalList = []


position_lists = [list1, list2, list3, list4, list5]

for position_list in position_lists: # for every position

    found_my_guy = False

    while not found_my_guy: # keep looping till I find my guy

        selectedPerson = position_list[ random.randint( 0,len(position_list) -1 ) ]

        if selectedPerson not in FinalList: # only append guys that are not duplicates
            FinalList.append(selectedPerson)
            found_my_guy = True # exit while loop and go to next `lineup'


for person in FinalList:
    sys.stdout.write(person + '\n')
like image 214
Derron R Avatar asked Feb 14 '23 13:02

Derron R


1 Answers

We can use itertools.product to generate the cartesian product of the lists then filter out any results that have duplicates:

from itertools import product

list1 = ['Gabe', 'Taylor', 'Kyle', 'Jay']
list2 = ['Gabe', 'Taylor', 'Kyle', 'Jay', 'James', 'John', 'Tyde','Chris', 'Bruno', 'David']
list3 = ['Gabe', 'Taylor', 'Kyle', 'Jay', 'James', 'John', 'Tyde','Chris', 'Bruno', 'David']
list4 = ['Kyle', 'James', 'John', 'Tyde','Bruno', 'Drew', 'Chris']
list5 = ['James', 'John', 'Brendan','Tim', 'Drew' ]

FinalList = []

for x in product(list1, list2, list3, list4, list5):
    # check for duplicates
    if len(set(x)) == 5 and set(x) not in FinalList:
        FinalList.append(set(x))


# to print
for x in FinalList:
    print x

There are more efficient ways to calculate such a list I believe, but this code runs basically instantly on my modest laptop.

Also, to address your second question, basically you were doing it the wrong way. In theory taking random guesses would allow you to create all possible sets of names, but only has you approached infinity. In practice it would of course be much sooner, but still a lot less efficient than just generating the list outright.

Edit: Also, as a final note:

>>> len(FinalList)
970

(This list may not be real helpful...)

like image 123
korylprince Avatar answered Feb 17 '23 02:02

korylprince