Help with regex needed

D'Arcy J.M. Cain darcy at druid.net
Tue Apr 12 15:48:58 EDT 2011


On Tue, 12 Apr 2011 22:11:55 +0300
Yuri Slobodyanyuk <yuri.slobodyanyuk at gmail.com> wrote:
> Thanks for the insight, while this code will run once a week and
> optimization isn't really a must here, it is
> still  a good idea not to leave half-baked code behind me, especially given
> that it will be running on this server  for the next  13 years ;)

You don't want to embarrass yourself in 13 years, eh?  :-)

> I have one doubt though . Doesn't using the list comprehension here increase
> number of loops per the same string ?

Nope.  It just optimizes the code a bit.

> for nnn in [x.split() for x in hhh]:
> My vision here is that after doing the comprehension it would look:
> for nnn in [1st_field,2nd_field,3rd_filed,...,nth_filed]:

  for nnn in hhh:
becomes
  for nnn in ('a b c', 'd e f', 'g h i'):  # loops three times

  for nnn in [x.split() for x in hhh]:
becomes
  for nnn in [['a','b','c'],['d','e','f'],['g','h','i']: # still three

> ... and therefore would do number of loops equal to number of fields while
> we really need just one ?

The number of loops is still the number of objects (strings) in hhh.
The only difference is that in the former case you need to split the
variable inside the loop causing the production and destruction of an
extra object.

Be careful though.  You can optimize your loops by doing too much in
the for statement.  You would be hard put to find a case where
optimization is worth more than readability.


-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list