test = ["a","b","c","d","e"]

def xuniqueCombinations(items, n):
    if n==0: yield []
    else:
        for i in xrange(len(items)-n+1):
            for cc in xuniqueCombinations(items[i+1:],n-1):
                yield [items[i]]+cc

x = xuniqueCombinations(test, 3)
print x

输出
"generator object xuniqueCombinations at 0x020EBFA8"

我想查看它找到的所有组合。我怎样才能做到这一点?

最佳答案

leoluk是正确的,您需要对其进行迭代。但这是正确的语法:

combos = xuniqueCombinations(test, 3)
for x in combos:
    print x

或者,您可以先将其转换为列表:
combos = list(xuniqueCombinations(test, 3))
print combos

关于python - 如何打印我认为是物体的物体?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3710823/

10-09 01:58