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?
To print the attributes of an object we can use “object. __dict__” and it return a dictionary of all names and attributes of object.
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.
In Python, you can print objects to the file by specifying the file parameter.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With