preallocate list

Steven Bethard steven.bethard at gmail.com
Wed Apr 13 11:17:11 EDT 2005


Jim wrote:
> Thanks for the suggestions. I guess I must ensure that this is my bottle 
> neck.
> <code>
>     def readFactorsIntoList(self,filename,numberLoads):
>     factors = []
>         f = open(self.basedir + filename,'r')
>         line = f.readline()
>         tokens = line.split()
>         columns = len(tokens)
>         if int(columns) == number:
>             for line in f:
>                 factor = []
>                 tokens = line.split()
>                 for i in tokens:
>                     factor.append(float(i))
>                 factors.append(loadFactor)
>         else:
>             for line  in f:
>                 tokens = line.split()
>                 factors.append([float(tokens[0])] * number)
>     return factors
> </code>
> 
> OK. I've just tried with 4 lines and the code works. With 11000 lines it 
> uses all CPU for at least 30 secs. There must be a better way.

Was your test on *just* this function?  Or were you doing something with 
the list produced by this function as well?

STeVe



More information about the Python-list mailing list