|
Re: Is there a better way to do this?: msg#00043python.numeric.general
Hee-Seng Kye wrote: My question is not directly related to NumPy, but since many people here deal with numbers, I was wondering if I could get some help; it would be even better if there is a NumPy (or Numarray) function that takes care of what I want! Sorry for taking a month. My solution is not faster than any of the ones that have already been proposed, but it is more elegant because it does not require the "n" nested for loops. Another hint is to use a generator for this purpose. Advantage is that the base and the number of digits can both be externally defined! Regards, Rob # As a reference, the general case where digits are not created in # order def gen(ndigits, base): dig = [0]*ndigits yield dig while 1: for num in range(ndigits-1,-1,-1): if dig[num] < base - 1: dig[num] += 1 break else: dig[num] = 0 else: return yield dig def genordered(ndigits, base): if ndigits > base: return dig = range(ndigits) yield dig while 1: for num in range(ndigits-1,-1,-1): if dig[num] < base-ndigits+num: dig[num] += 1 for num2 in range(num+1,ndigits): dig[num2] = dig[num2-1] + 1 break else: return yield dig c = 1 for dig in genordered(ndigits = 6, base = 12): # This is the same as your print statement, but again independent of # the number of digits print "%3d\t"%c,' '.join(map(lambda x: "%X"%x, dig)) c += 1 print "...Done" ------------------------------------------------------- SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media 100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33 Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift. http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285 |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: Using sum() within a function: 00043, Jim Cser |
|---|---|
| Next by Date: | RE: Using sum() within a function: 00043, Perry Greenfield |
| Previous by Thread: | Using sum() within a functioni: 00043, Jim Cser |
| Next by Thread: | ANN: Python/Scientific Job openings at Enthought: 00043, eric jones |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |