preallocate list

Jim jbo at cannedham.ee.ed.ac.uk
Wed Apr 13 11:46:53 EDT 2005


peufeu at free.fr wrote:
> 
> what about :
> 
> factors = [map(float, line.split()) for line in file]
> 
> should be a hell of a lot faster and nicer.
> 
>>              for line in f:
>>                  factor = []
>>                  tokens = line.split()
>>                  for i in tokens:
>>                      factor.append(float(i))
>>                  factors.append(factor)
>>
>> Is this nasty?
>>
>> Jim
> 
> 
Oh the relief :)

Of course, line.split() is already a list.

Couple of seconds for the 10000 line file.

Thanks.

What I really want is a Numeric array but I don't think Numeric supports 
importing files.

Jim



More information about the Python-list mailing list