[Numpy-discussion] combinations anomaly

Alan G Isaac aisaac at american.edu
Sat Sep 22 20:15:34 EDT 2007


Charles harris posted a generator function for generating 
combinations (based on Knuth's fascicle).  I get the 
expected results by iterating over the resulting generator,
but not if I let ``list`` do that for me.  What is more, 
changing ``arange`` to ``range`` in the code eliminates
this anomaly.

What am I failing to understand here?

Thank you,
Alan Isaac


################  example ###############################
def combinations(n,t):
    c = arange(t + 2)
    c[-2] = n
    c[-1] = 0
    while 1:
        yield c[:t]
        j = 0
        while c[j] + 1 == c[j+1] :
            c[j] = j
            j += 1
        if j >= t :
            return
        c[j] += 1

print "I get the expected results by iterating:"

for tpl in combinations(5,3):
    print tpl

print "However list conversion fails:"

print list(combinations(5,3))
############# end example ###############################





More information about the NumPy-Discussion mailing list