[Tutor] Graphing the random.gauss distribution
Luke Paireepinart
rabidpoobear at gmail.com
Tue Aug 14 19:00:43 CEST 2007
>> 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..)
>
No, of course not!
"wherever possible" would include
[foo(25) for x in range(300)]
in order to call foo 300 times. This is obviously a bad idea.
Basically you use a list comprehension when you're building a list in a
simple way.
If it's more clear as a for loop, write it as a for loop.
It's up to your consideration if the situation would benefit in
readability from a list comp.
>> but there is no need to keep the array index in the array so this is
>> simpler:
>>
>> d = [0] * 200
>>
>>
>>> for k in xrange(gaussCalls):
>>> n = int(gauss(mean, std))
>>> d[n][1] += 1
>>>
>> This becomes just
>> d[n] += 1
>>
>>
>>> for c in d:
>>> barLength = c[1]//barLengthAdjuster
>>> print barLength, "=", c[0], c[1]
>>>
>
> By the time my code got into my post, I had changed "print barLength
> * "=", c[0], c[1]" to "print barLength, "=", c[0], c[1]", thinking
> upon reading it over that the "*" was a mistake. :-( The code I
> didn't send DID make bars out of "="s.
>
Sure it did ;)
>> Use enumerate() to get the indices as well as the list contents. This
>> version prints an actual bar as well:
>> for i, count in enumerate(d):
>> barLength = count//barLengthAdjuster
>> print i, '*' * barLength, count
>>
>
> Ah, enumerate() is nice! I'd forgotten about it. And "*" IS better
> for bars than "=".
>
> 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).
>
well, you already answered this yourself.
> So this is my code now:
>
> ====================================
> from random import gauss
> mean = 100
> std = 10
> gaussCalls =1000000
> barLengthAdjuster = gaussCalls//2600
>
> d = [0] * 200
>
> for k in xrange(gaussCalls):
> n = int(gauss(mean, std))
> d[n] += 1
>
> for i, count in enumerate(d):
> barLength = count//barLengthAdjuster
> print '*' * barLength, i, count
> =====================================
>
> 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)
>
> Thanks very much, Kent, for taking the time to advise me on my code.
>
> Dick
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list