align on char

Peter Otten __peter__ at web.de
Thu Jun 17 17:31:02 EDT 2004


Batista, Facundo wrote:

> My way:
> 
>>>> lines = (
>   'user1 : John Hunter',
>   'another user : Bill Bragg',
>   'yet on more : Hi There',
> )
> 
>>>> lines
> ('user1 : John Hunter', 'another user : Bill Bragg', 'yet on more : Hi
> There')
>>>> aligned = []
>>>> maxa = 0
>>>> for line in lines:
>         (a, b) = line.split(':')
>         maxa = max(maxa, len(a))
>         aligned.append(':'.join((a.ljust(maxa),b)))

I think you are cheating. You don't know maxa until the loop has finished.

Peter




More information about the Python-list mailing list