How to create all possible unique lists

I am writing a short program for my basketball team. I got a coach to divide the players into lists corresponding to a specific position. (List1 = Point Guard)

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

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

How can I get this to loop in such a way that it prints out all the "Real" configurations of 5 players?

Any suggestions or directions are welcome!

Here is what I still have:

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')
+4
source share
4 answers
l = [(a,b,c,d,e) for a in list1 
                    for b in list2 
                        for c in list3 
                            for d in list4 
                                for e in list5 
                                    if len(set((a,b,c,d,e))) == 5]
s = set(map(lambda e: tuple(sorted(e)), l))
print len(s)

>>> 970

EDIT: ,

s = set([frozenset((a,b,c,d,e)) for a in list1 
                for b in list2 
                    for c in list3 
                        for d in list4 
                            for e in list5 
                                if len(set((a,b,c,d,e))) == 5])
print len(s)

>>> 970
+2

itertools.product , :

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

, , .

, , . , . , , , , .

: , :

>>> len(FinalList)
970

( ...)

+4

if s2 in FinalList, , s2 FinalList,

+1
playerLists = tuple(list1, list2, list3, list4, list5)
masterSet = set(list1 + list2 + list3 + list4 + list5)

from random import choice
def FindPlayer(playerList):
    while True:
        randomPlayer = choice(playerList)
        if randomPlayer in masterSet:
            masterSet.remove(randomPlayer)
            return randomPlayer

for playerList in playerLists:
    print FindPlayer(playerList)
+1
source

All Articles