如何创建所有可能的唯一列表

4
我正在为我的篮球队编写一个简短的程序。我已经让教练将球员分成了对应特定位置的列表。(List1 = 控球后卫)
利用这些列表,我想创建一个包含所有可能“有效”阵容的输出。
目前,我已经编写了一个基本程序,可以从每个列表中选择5个独特的人员。
如何使其循环以便打印出所有“有效”的5名球员组合?
非常感谢任何建议或指导!
以下是我目前已经完成的内容:
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')

如果有人对后续问题有任何建议,请查看这里:http://stackoverflow.com/questions/20234030/keeping-only-unique-instances-of-lists-whose-only-difference-is-order - Derron R
4个回答

4
我们可以使用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

(这个列表可能并不真正有用...)


我该如何打印最终列表的内容?我不确定在Python中如何知道某个东西是什么... - Derron R
请查看更新后的代码,以了解逐行打印的示例。 - korylprince
我认为这个答案是错误的,同样的集合可以出现多次。 - Guy Gavriely
所有的列表都是独一无二的,但它们不是集合。请查看此处的后续问题:http://stackoverflow.com/questions/20234030/keeping-only-unique-instances-of-lists-whose-only-difference-is-order - Derron R

2
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

编辑:或许更好的方法

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

a != b != c != d != e并不意味着a-e是唯一的。尝试1 != 2 != 1。(结果为True - korylprince
我该怎么处理另一个问题?我不能删除它,因为它有答案。 - Derron R
我使用了以下代码:for lineup in s: print(lineup)有没有一种简单的方法可以使输出始终按照列表的顺序呈现? - Derron R
应该使用“print s” - Guy Gavriely

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
使用 if s2 in FinalList 来检查 s2 是否已经在 FinalList 中被选择。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接