开发者

How to print what I think is an object?

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

outputs

开发者_运维问答"generator object xuniqueCombinations at 0x020EBFA8"

I want to see all the combinations that it found. How can i do that?


leoluk is right, you need to iterate over it. But here's the correct syntax:

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

Alternatively, you can convert it to a list first:

combos = list(xuniqueCombinations(test, 3))
print combos


This is a generator object. Access it by iterating over it:

for x in xuniqueCombinations:
    print x


x = list(xuniqueCombinations(test, 3))
print x

convert your generator to list, and print......


It might be handy to look at the pprint module: http://docs.python.org/library/pprint.html if you're running python 2.7 or more:

from pprint import pprint
pprint(x)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜