At 10:28 AM 8/14/2007, you wrote: >Dick Moores wrote: > > At 06:47 AM 8/14/2007, Kent Johnson wrote: > >> This could be a list comprehension: > >> d = [ [k, 0] for k in range(200) ] > > > > So you recommend using list comprehensions wherever possible? (I sure > > wouldn't have thought of that one..) > >Not "whenever possible", no, but I find simple list comps (I count this >one as simple) to be far more readable than the equivalent loop. Not >only are they shorter but they read the way I think. > >If the list comp can't be easily written on one line, or has a complex >condition, or has two for clauses, I find it less appealing and may >write it as a for loop. I never use a list comp just for the >side-effects; only when I actually want the list.
Got it. > > I prefer the index (or integer) to come after the bar ends, and before > > the count. One reason is that if the index is at the base of the bar, at > > 100 and above, the bars get pushed out one character longer than they > > should be relative to the 99 or less bars. I suppose there's a way to > > handle this, but I couldn't think of it then (but see below). > >Use string formatting or str.rjust(): >In [1]: '%3d' % 10 >Out[1]: ' 10' >In [2]: '%3d' % 100 >Out[2]: '100' >In [4]: str(10).rjust(3) >Out[4]: ' 10' So: for i, count in enumerate(d): barLength = count//barLengthAdjuster print "%3d %s %d" % (i, '*' * barLength, count) Or: for i, count in enumerate(d): barLength = count//barLengthAdjuster print str(i).rjust(3), '*' * barLength, count Right? (Anyway, they work!) Terrific! Two ways! > > This would solve the problem I mentioned above caused by putting the > > indices at the bases of the bars: > > > > for i, count in enumerate(d): > > barLength = count//barLengthAdjuster > > if i < 100: > > print "%d %s %d" % (i, '*' * barLength, count) # there are 2 > > spaces between %d and %s > > else: > > print "%d %s %d" % (i, '*' * barLength, count) > >Ouch. See above. Ouch? No like? (I know, your 2 ways are both easier.) Thanks much again, Kent. Dick _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor