Line breaks in list causing a small formatting problem while joining the list
GrayShark
howe.steven at gmail.com
Fri Jan 21 16:04:24 EST 2011
On Fri, 21 Jan 2011 07:39:26 -0800, Oltmans wrote:
> Hi Python gurus, hope you're doing well. I've a small problem.
>
> When I run the following code
> ___________________________________________________
>>>> names = ['oltmans','abramhovic','\n','sal','lee'] print '| ' + ' |
>>>> '.join(names)
> | oltmans | abramhovic |
> | sal | lee
> ___________________________________________________
>
> I get the output like above. However, I want it to output like below
>
> | oltmans | abramhovic |
> | sal | lee
>
>
> That is, there shouldn't be a space in the beginning of second line. The
> list can of course contain more than 5 elements. Any ideas? I will
> appreciate any hint. Thanks in advance.
It looks like your trying to print a formatted list.
With your code you are:
1) creating a string from a list, with added characters.
2) printing the new string.
So, look at your string:
names = ['oltmans','abramhovic','\n','sal','lee']
newNames = '| ' + ' | '.join( names )
>> newNames
'| oltmans | abramhovic | \n | sal | lee'
Now you can see your space after the newline (and a missing pipe symbol at
the end).
When you ask the compiler for newNames, you can see there is a space after
the newline character. Naturally, the print operator prints out the space.
If this is indeed a formatted list, you should try something else.
Something like:
# first get rid of you formatting element in the list '\n'.
names = [ 'oltmans','abramhovic','sal','lee' ]
# next iterate by twos via the function 'range( start, stop, step )'
range( 0, len( names ), 2 )
[ 0, 2 ]
# now fix up the printing by twos.
>>> for x in range( 0, len( names ), 2 ):
... print '| %s | %s |' % ( names[ x ], names[ x + 1 ] )
...
| oltmans | abramhovic |
| sal | lee |
Next, make it pretty.
The next step would be to find the longest string in your list.
>>> def max( theList ):
... theMax = 0
... for element in theList:
... if len( element ) > theMax:
... theMax = len( element )
... return theMax
>>> max( names )
10
Now some centering of strings, from you list.
>>> for x in range( 0, len( names ), 2 ):
... print '| %s | %s |' % \
( names[ x ].center(10), \
names[ x +1 ].center(10) )
...
| oltmans | abramhovic |
| sal | lee |
Pretty list.
Now make it obscure, like you are a perl programmer; don't
forget to eat up memory as you go along ....
def maxElement( aList ):
lenList = []
for x in aList: lenList.append( len( x ) )
return sorted( lenList, reverse=True )[0]
def formatLine( firstName, secondName, width ):
return '| %s | %s | % \
( firstName.center( width ), \
secondName.center( width ) )
theWidth = maxElement( names )
for x in range( 0, len( names ), 2 ):
aLine = formatLines( names[x], names[x+1], theWidth )
print aLine
Make sure to create at lest two additions files to store
maxElement and formatLine, create an __init__.py and make a package,
turn in the project and get expelled for being grandiose.
steven.
More information about the Python-list
mailing list