[Tutor] Making table
Carroll, Barry
Barry.Carroll at psc.com
Wed Mar 21 18:33:00 CET 2007
> -----Original Message-----
> Date: Tue, 20 Mar 2007 22:53:15 +0100
> From: J?nos Juh?sz <janos.juhasz at VELUX.com>
> Subject: Re: [Tutor] Making table
> To: tutor at python.org
> Message-ID:
> <OF6CDC76A3.A1E29388-ONC12572A4.0077642D-
> C12572A4.00783AB2 at velux.com>
> Content-Type: text/plain; charset="iso-8859-2"
>
> Dear Barry,
>
> >>Using a formatting string of "%10.4f", these would be rendered as:
> >>
> >> ' 253.0000'
> >> ' 77.6000
> >> ' 9.0300'
> >> ' 0.0210'
> >>
> >>This formatting gives the impression that all values but the last
are
> >>more precise than they truly are. A scientist or statistician would
> >>prefer to see something like this:
> >>
> >> '254. '
> >> ' 77.6 '
> >> ' 9.03 '
> >> ' 0.0210'
> >>
> >>Does numpy or some other math package have that capability?
>
> You can use advanced regexp, called lookaround, lookbehind for this
> purpose.
>
> ###############
>
> import re
>
> l = (253., 77.6, 9.03, .0210, 1000, 100.1230)
> ending_zero = re.compile('0(?=0*$)') # zero followed with only zeros
>
> for f in l:
> print re.sub(ending_zero, ' ', ('%10.4f' % f))
>
> ###############
>
>
>
>
> Yours sincerely,
> ______________________________
> J?nos Juh?sz
Thanks, J?nos. Your solution works in almost all cases. It breaks down
when one or more trailing zeros are significant: 0.0210 should print
unchanged, since the final 0 is significant.
Still this is a big step forward. It shouldn't be too hard to add the
additional logic.
Regards,
Barry
barry.carroll at psc.com
541-302-1107
________________________
We who cut mere stones must always be envisioning cathedrals.
-Quarry worker's creed
More information about the Tutor
mailing list