preallocate list

Mike C. Fletcher mcfletch at rogers.com
Wed Apr 13 09:46:22 EDT 2005


Jim wrote:

> Thanks for the suggestions. I guess I must ensure that this is my
> bottle neck.

...

>             for line in f:
>                 factor = []
>                 tokens = line.split()
>                 for i in tokens:
>                     factor.append(float(i))
>                 factors.append(loadFactor)
>
...

You might try:

    factors = [ [float(item) for item in line.split()] for line in f ]

avoiding the extra statements for appending to the lists.  Also might try:

    factors = [ map(float, line.split()) for line in f ]

though it uses the out-of-favour functional form for the mapping.

Good luck,
Mike

________________________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com




More information about the Python-list mailing list