Re: istep() addition to itertool? (Was: Re: Printing n elements per line in a list)

2006-08-21 Thread Neil Cerutti
On 2006-08-19, Rhamphoryncus <[EMAIL PROTECTED]> wrote: > unexpected wrote: >> If have a list from 1 to 100, what's the easiest, most elegant >> way to print them out, so that there are only n elements per >> line. > > I've run into this problem a few times, and although many > solutions have been

Re: istep() addition to itertool? (Was: Re: Printing n elements per line in a list)

2006-08-20 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Justin Azoff wrote: > Rhamphoryncus wrote: > [snip interesting istep function] > >> Would anybody else find this useful? Maybe worth adding it to itertool? > > yeah, but why on earth did you make it so complicated? > > def istep(iterable, step): > a=[] > for x

Re: istep() addition to itertool? (Was: Re: Printing n elements per line in a list)

2006-08-20 Thread Justin Azoff
Rhamphoryncus wrote: > I've run into this problem a few times, and although many solutions > have been presented specifically for printing I would like to present a > more general alternative. [snip interesting istep function] > Would anybody else find this useful? Maybe worth adding it to itert

istep() addition to itertool? (Was: Re: Printing n elements per line in a list)

2006-08-19 Thread Rhamphoryncus
unexpected wrote: > If have a list from 1 to 100, what's the easiest, most elegant way to > print them out, so that there are only n elements per line. I've run into this problem a few times, and although many solutions have been presented specifically for printing I would like to present a more g

Re: Printing n elements per line in a list

2006-08-17 Thread Neil Cerutti
On 2006-08-15, unexpected <[EMAIL PROTECTED]> wrote: > If have a list from 1 to 100, what's the easiest, most elegant > way to print them out, so that there are only n elements per > line. > > So if n=5, the printed list would look like: > > 1 2 3 4 5 > 6 7 8 9 10 > 11 12 13 14 15 > etc. > > My sea

Re: Printing n elements per line in a list

2006-08-16 Thread John Machin
Matimus wrote: > Well, I have another at bat, so I will try to redeem myself... using > recursion: > > def printTable(l,c): > print(("%d "*len(l[:c]))%tuple(l[:c])) > if( len(l[:c]) > 0 ): > printTable(l[c:],c) > > printTable(range(1,101),5) Sorry. Recursion disqualified your bat

Re: Printing n elements per line in a list

2006-08-16 Thread johnzenger
def perline(n): count = 1 while 1: yield (count == n) and "\n" or " " count = count % n + 1 r = range(1,101) p = perline(5) print "".join("%d%s" % (x, p.next()) for x in r) unexpected wrote: > If have a list from 1 to 100, what's the easiest, most elegant way to > print t

Re: Printing n elements per line in a list

2006-08-16 Thread Matimus
John Machin wrote: > Matimus wrote: > > unexpected wrote: > > > If have a list from 1 to 100, what's the easiest, most elegant way to > > > print them out, so that there are only n elements per line. > > > > > > So if n=5, the printed list would look like: > > > > > > 1 2 3 4 5 > > > 6 7 8 9 10 > >

Re: Printing n elements per line in a list

2006-08-16 Thread John Machin
Matimus wrote: > unexpected wrote: > > If have a list from 1 to 100, what's the easiest, most elegant way to > > print them out, so that there are only n elements per line. > > > > So if n=5, the printed list would look like: > > > > 1 2 3 4 5 > > 6 7 8 9 10 > > 11 12 13 14 15 > > etc. > > > > My

Re: Printing n elements per line in a list

2006-08-16 Thread Matimus
unexpected wrote: > If have a list from 1 to 100, what's the easiest, most elegant way to > print them out, so that there are only n elements per line. > > So if n=5, the printed list would look like: > > 1 2 3 4 5 > 6 7 8 9 10 > 11 12 13 14 15 > etc. > > My search through the previous posts yields

Re: Printing n elements per line in a list

2006-08-16 Thread John Machin
Yu-Xi Lim wrote: > John Machin wrote: > > > How did you avoid division when testing for leap year? > > > > Well, you're testing for a modulus, not really a division, so a nasty hack: > > def isleapyear(year): > return not year&3 > > works for 1901-2099. > > But programming like this causes a

Re: Printing n elements per line in a list

2006-08-16 Thread Sion Arrowsmith
In article <[EMAIL PROTECTED]>, Dan Sommers <[EMAIL PROTECTED]> wrote: >Perhaps not the prettiest, but I can't think of anything simpler to read >six months from now: > >counter = 0 >for an_element in the_list: >print an_element, >counter = counter + 1 >if counter =

Re: Printing n elements per line in a list

2006-08-16 Thread Gerard Flanagan
John Machin wrote: > Gerard Flanagan wrote: > > > > > just variations on previous answers: > > > > rng = range(1,101) > > > > #ad hoc > > for line in ( rng[i:i+5] for i in xrange(0,100,5) ): > > print ' '.join(map(str,line)) > > > > #in general > > def lines( seq, count=1 ): > > n = len(se

Re: Printing n elements per line in a list

2006-08-16 Thread John Machin
Gerard Flanagan wrote: > > just variations on previous answers: > > rng = range(1,101) > > #ad hoc > for line in ( rng[i:i+5] for i in xrange(0,100,5) ): > print ' '.join(map(str,line)) > > #in general > def lines( seq, count=1 ): > n = len(seq) > for x in ( seq[i:i+count] for i in xr

Re: Printing n elements per line in a list

2006-08-16 Thread Gerard Flanagan
unexpected wrote: > If have a list from 1 to 100, what's the easiest, most elegant way to > print them out, so that there are only n elements per line. > > So if n=5, the printed list would look like: > > 1 2 3 4 5 > 6 7 8 9 10 > 11 12 13 14 15 > etc. > > My search through the previous posts yield

Re: Printing n elements per line in a list

2006-08-15 Thread John Machin
Dan Sommers wrote: > On 15 Aug 2006 18:33:51 -0700, > "John Machin" <[EMAIL PROTECTED]> wrote: > > ... ctr = (ctr + 1) % n > > I'm old enough to remember the days when we avoided division like the > plague. Remember those zippy 1MHz (yes, that's an M and not a G) CPUs > with less than a

Re: Printing n elements per line in a list

2006-08-15 Thread Steven D'Aprano
On Tue, 15 Aug 2006 19:49:17 -0700, John Machin wrote: > Ugliness is in the eye of the beholder. > It is more blessed to add than to multiply. > Gaze upon this alternative: > > def printitems2(sequence, count=5): > """Print count items of sequence per line.""" > for pos in range(0, len(se

Re: Printing n elements per line in a list

2006-08-15 Thread Dan Sommers
On 15 Aug 2006 18:33:51 -0700, "John Machin" <[EMAIL PROTECTED]> wrote: > Dan Sommers wrote: >> >> counter = 0 >> for an_element in the_list: >> print an_element, >> counter = counter + 1 >> if counter == n: >> print >> counter = 0 >> > Yes, often simple old-fashioned ways are the best. A litt

Re: Printing n elements per line in a list

2006-08-15 Thread Simon Forman
John Machin wrote: > Steven D'Aprano wrote: > > I think it's possible to > > > hack it up using while loops and some ugly slicing, but hopefully I'm > > > missing something > > > > I don't see why you think that's an ugly hack. > > > > def printitems(sequence, count=5): > > """Print count items

Re: Printing n elements per line in a list

2006-08-15 Thread John Machin
Steven D'Aprano wrote: > I think it's possible to > > hack it up using while loops and some ugly slicing, but hopefully I'm > > missing something > > I don't see why you think that's an ugly hack. > > def printitems(sequence, count=5): > """Print count items of sequence per line.""" > numr

Re: Printing n elements per line in a list

2006-08-15 Thread Steven D'Aprano
On Tue, 15 Aug 2006 16:51:29 -0700, unexpected wrote: > If have a list from 1 to 100, what's the easiest, most elegant way to > print them out, so that there are only n elements per line. > > So if n=5, the printed list would look like: > > 1 2 3 4 5 > 6 7 8 9 10 > 11 12 13 14 15 > etc. > > My

Re: Printing n elements per line in a list

2006-08-15 Thread John Machin
Dan Sommers wrote: > > counter = 0 > for an_element in the_list: > print an_element, > counter = counter + 1 > if counter == n: > print > counter = 0 > Yes, often simple old-fashioned ways are the best. A little verbose, though. And I'd do

Re: Printing n elements per line in a list

2006-08-15 Thread Simon Forman
unexpected wrote: > If have a list from 1 to 100, what's the easiest, most elegant way to > print them out, so that there are only n elements per line. > > So if n=5, the printed list would look like: > > 1 2 3 4 5 > 6 7 8 9 10 > 11 12 13 14 15 > etc. > > My search through the previous posts yields

Re: Printing n elements per line in a list

2006-08-15 Thread Dan Sommers
On 15 Aug 2006 16:51:29 -0700, "unexpected" <[EMAIL PROTECTED]> wrote: > If have a list from 1 to 100, what's the easiest, most elegant way to > print them out, so that there are only n elements per line. > So if n=5, the printed list would look like: > 1 2 3 4 5 > 6 7 8 9 10 > 11 12 13 14 15 >

Re: Printing n elements per line in a list

2006-08-15 Thread Paul Rubin
Paul Rubin writes: >for x,y in itertools.groupby(a, lambda n: n//5): >print ' '.join(str(k) for k in y) > > I hope I got that right. Of course I meant to say >for x,y in itertools.groupby(enumerate(a), lambda (n,x): n//5): >print ' '.join(str(k)

Re: Printing n elements per line in a list

2006-08-15 Thread Paul Rubin
"unexpected" <[EMAIL PROTECTED]> writes: > If have a list from 1 to 100, what's the easiest, most elegant way to > print them out, so that there are only n elements per line. > > So if n=5, the printed list would look like: > > 1 2 3 4 5 > 6 7 8 9 10 > 11 12 13 14 15 > etc. > > My search throug

Printing n elements per line in a list

2006-08-15 Thread unexpected
If have a list from 1 to 100, what's the easiest, most elegant way to print them out, so that there are only n elements per line. So if n=5, the printed list would look like: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 etc. My search through the previous posts yields methods to print all the values of t