Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 996
Alex Avatar asked Sep 14 '10 16:09

Alex


People also ask

How do I print all attributes of an object?

To print the attributes of an object we can use “object. __dict__” and it return a dictionary of all names and attributes of object.

How do you print the data of an object in Python?

Use Python's vars() to Print an Object's Attributes The dir() function, as shown above, prints all of the attributes of a Python object. Let's say you only wanted to print the object's instance attributes as well as their values, we can use the vars() function.

Can you print an object in Python?

In Python, you can print objects to the file by specifying the file parameter.


1 Answers

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
like image 192
Colin Gislason Avatar answered Oct 09 '22 15:10

Colin Gislason