[Tutor] Need to be taught a trick or two about printing in neat columns
Markus Rosenstihl
markusro at element.fkp.physik.tu-darmstadt.de
Thu Nov 2 20:33:00 CET 2006
Am 02.11.2006 um 15:14 schrieb Dick Moores:
> At 03:31 AM 11/2/2006, you wrote:
>> At 03:13 AM 11/2/2006, Kent Johnson wrote:
>>> Luke Paireepinart wrote:
>>>> Instead of helping you with your specific problem, I'll give you
>>>> this
>>>> information and see what you can make of it.
>>>>
>>>>>>> print 'a'.ljust(20)+'b'.ljust(20)
>>>> a b
>>>>>>> print 'carrah'.ljust(20)+'foobar'.ljust(20)
>>>> carrah foobar
>>>
>>> Another way to do this is with string formatting, I think it is a
>>> more
>>> readable and flexible solution:
>>>
>>> In [1]: print '%-20s %-20s' % ('a', 'b')
>>> a b
>>>
>>> In [2]: print '%-20s %-20s' % ('carrah', 'foobar')
>>> carrah foobar
>>>
>>> See this page for details:
>>> http://docs.python.org/lib/typesseq-strings.html
>>
>> Thanks, Kent. I agree. So now that function has become
>>
>> def printListOfAllUnitsAndAbbreviations():
>> """
>> Prints a 3-column list of all units and their abbreviations,
>> but not their categories.
>> """
>> lstAll = allUnitsAndTheirAbbreviationsAndCategories()
>> for i in range(0, len(lstAll)-1 ,3):
>> print '%-27s %-27s %-27s' % (lstAll[i][2:],
>> lstAll[i+1][2:], lstAll[i+2][2:])
>> print
>
> Oops! Got overconfident. Didn't check to see if it actually printed
> the whole list. It didn't. Left off "rad: radian", because there are
> 46 items in the list (46%3 is 1, not 0). So now the only way I could
> see to print all 46 was to add 2 empty dummies to make 48, which is
> divisible by 3, and also modify the range in the second function. Is
> there a better way, which is also a general solution that will work
> when I subtract or add to the list? See the two modified functions
> below.
>
> Dick
>
>
> def allUnitsAndTheirAbbreviationsAndCategories():
> """
> A list of all units, their abbreviations and categories.
> """
> abbs = [
> 'l mi: mile',
> 'l km: kilometer',
> 'l m: meter',
> 'l yd: yard',
> 'l ft: foot',
> 'l in: inch',
> 'l cm: centimeter',
> 'l mm: millimeter',
> 'l fur: furlong',
> 'l lea: league',
> 'l nm: nautical mile',
> 'a ha: hectare',
> 'a ac: acre',
> 'a mi2: square mile',
> 'a km2: square kilometer',
> 'a m2: square meter',
> 'a yd2: square yard',
> 'a ft2: square foot',
> 'a in2: square inch',
> 'a cm2: square centimeter',
> 'w kg: kilogram',
> 'w lb: pound',
> 'w gm: gram',
> 'w oz: ounce',
> 'v qt: quart',
> 'v oz: ounce',
> 'v l: liter',
> 'v ml: milliliter',
> 'v gal: gallon',
> 'v tbsp: tablespoon',
> 'v tsp: teaspoon',
> 'v impgal: Imperial Gallon',
> 'v yd3: cubic yard',
> 'v m3: cubic meter',
> 'v ft3: cubic foot',
> 'v mi3: cubic mile',
> 'v km3: cubic kilometer',
> 't F: Fahrenheit',
> 't C: Celsius',
> 't K: Kelvin',
> 's mph: miles per hour',
> 's knots: knots',
> 's mps: miles per second',
> 's fps: feet per second',
> 'd deg: degree',
> 'd rad: radian',
> ' ',
> ' '
> ]
> return abbs
>
> def printListOfAllUnitsAndAbbreviations():
> """
> Prints a 3-column list of all units and their abbreviations,
> but not their categories.
> """
> lstAll = allUnitsAndTheirAbbreviationsAndCategories()
> for i in range(0, len(lstAll), 3):
> print '%-27s %-27s %-27s' % (lstAll[i][2:],
> lstAll[i+1][2:], lstAll[i+2][2:])
> print
>
Try somthing like this:
In [32]: a=range(100)
In [33]: for i in range(0,len(a)):
....: print '%-27s'%a[i],
....: if (i+1)%3 == 0: print "\n"
0 1 2
3 4 5
...
93 94 95
96 97 98
99
Note the comma after the first print statement
Regards
Markus
More information about the Tutor
mailing list