[Tutor] Help with strings and lists.

János Juhász janos.juhasz at VELUX.com
Fri Jul 14 12:12:42 CEST 2006


Dear Alan,

Probably you will be interested about list comprehension and zip(), as it 
can simplify all the similar tasks.

>>> s = ('Monday 7373 3663657 2272 547757699 reached 100%','Tuesday 
7726347 552 766463 2253 under-achieved 0%','Wednesday 9899898 8488947 6472 
77449 reached 100%','Thursday 636648 553 22344 5699 under-achieved 
0%','Friday 997 3647757 78736632 357599 over-achieved 200%')
>>> # I made a table from your list
>>> table = [line.split() for line in s]
>>> # it is possible to transpose the table
>>> transposed = zip(*(table))
>>> transposed
[('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'), ('7373', 
'7726347', '9899898', '636648', '997'), ('3663657', '552', '8488947', 
'553', '3647757'), ('2272', '766463', '6472', '22344', '78736632'), 
('547757699', '2253', '77449', '5699', '357599'), ('reached', 
'under-achieved', 'reached', 'under-achieved', 'over-achieved'), ('100%', 
'0%', '100%', '0%', '200%')]
>>> # calc the max(len(str(cell))) for each row,
>>> # that means columns in the original table
>>> maxWidths = [max([len(str(cell)) for cell in column]) for column in 
transposed]
>>> maxWidths
[9, 7, 7, 8, 9, 14, 4]
>>> # format it
>>> [ str.ljust(str(field),w) for (field, w) in zip(table[0], maxWidths)]
['Monday   ', '7373   ', '3663657', '2272    ', '547757699', 'reached  ', 
'100%']
>>> # join it to give a line
>>> '|'.join([ str.ljust(str(field),w) for (field, w) in zip(table[0], 
maxWidths)])
'Monday   |7373   |3663657|2272    |547757699|reached       |100%'
>>> # it can be made for all of the lines
>>> '\n'.join(['|'.join([ str.ljust(str(field),w) for (field, w) in 
zip(line, maxWidths)]) for line in table])
'Monday   |7373   |3663657|2272    |547757699|reached       |100%\nTuesday 
 |7726347|552    |766463  |2253     |under-achieved|0% 
\nWednesday|9899898|8488947|6472    |77449    |reached |100%\nThursday 
|636648 |553    |22344   |5699     |under-achieved|0%  \nFriday   |997 
|3647757|78736632|357599   |over-achieved |200%'
>>> # and can be printed in this form
>>> print '\n'.join(['|'.join([ str.ljust(str(field),w) for (field, w) in 
zip(line, maxWidths)]) for line in table])
Monday   |7373   |3663657|2272    |547757699|reached       |100%
Tuesday  |7726347|552    |766463  |2253     |under-achieved|0% 
Wednesday|9899898|8488947|6472    |77449    |reached       |100%
Thursday |636648 |553    |22344   |5699     |under-achieved|0% 
Friday   |997    |3647757|78736632|357599   |over-achieved |200%
>>> 

I know it is a different way of thinking, but it works well with python.
It is the functional way instead of your procedural one.

> Hi,

> I do a far bit of data manipulation and decided to try one of my
> favourite utilities in Python. I'd really appreciate some optimization
> of the script. I'm sure that I've missed many tricks in even this short
> script.

> Let's say you have a file with this data:

> Monday 7373 3663657 2272 547757699 reached 100%
> Tuesday 7726347 552 766463 2253 under-achieved 0%
> Wednesday 9899898 8488947 6472 77449 reached 100%
> Thursday 636648 553 22344 5699 under-achieved 0%
> Friday 997 3647757 78736632 357599 over-achieved 200%

> You now want columns 1, 5, and 7 printed and aligned (much like a
> spreadsheet). For example:

> Monday    547757699 100%
> Wednesday     77449 100%
> ...

> This script does the job, but I reckon there are better ways.  In the
> interests of brevity, I have dropped the command-line argument handling
> and hard-coded the columns for the test and I hard-coded the input file
> name.


> Any help greatly appreciated.
> Regards,
> Alan.

Best Regards,
János Juhász
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060714/690ab168/attachment.htm 


More information about the Tutor mailing list