Alex
Alex

Reputation: 347

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?

Upvotes: 11

Views: 9701

Answers (4)

shahjapan
shahjapan

Reputation: 14335

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

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

Upvotes: 0

Colin Gislason
Colin Gislason

Reputation: 5589

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

Upvotes: 17

cpf
cpf

Reputation: 1501

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)

Upvotes: -3

leoluk
leoluk

Reputation: 12971

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

for x in xuniqueCombinations:
    print x

Upvotes: 4

Related Questions