John Machin wrote:
On Wed, 13 Apr 2005 14:28:51 +0100, Jim [EMAIL PROTECTED]
wrote:
Thanks for the suggestions. I guess I must ensure that this is my bottle
neck.
code
def readFactorsIntoList(self,filename,numberLoads):
1. numberLoads is not used.
factors = []
f =
Hi all
Is this the best way to preallocate a list of integers?
listName = range(0,length)
What about non integers?
I've just claimed in the newsgroup above that pre-allocating helps but I
might be getting confused with matlab ;)
If I have a file with a floating point number on each line, what is
Jim wrote:
If I have a file with a floating point number on each line, what is the
best way of reading them into a list (or other ordered structure)?
I was iterating with readline and appending to a list but it is taking
ages.
Perhaps you should use readlines (notice the s) instead of readline.
On 4/13/05, Jim [EMAIL PROTECTED] wrote:
Hi all
Is this the best way to preallocate a list of integers?
listName = range(0,length)
the 0 is unnecessary; range(length) does the same thing.
What about non integers?
arr = [myobject() for i in range(length)]
I've just claimed in the
rbt wrote:
Jim wrote:
If I have a file with a floating point number on each line, what is
the best way of reading them into a list (or other ordered structure)?
I was iterating with readline and appending to a list but it is taking
ages.
Perhaps you should use readlines (notice the s) instead
Just a correction:
snip
I would profile your app to see that it's your append which is taking
ages, but to preallocate a list of strings would look like:
[This is an average length string for i in range(approx_length)]
My guess is that it won't help to preallocate, but time it and let us
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)
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))
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))
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 =
Steven Bethard wrote:
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()
[EMAIL PROTECTED] 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:
Steven Bethard wrote:
Jim wrote:
..
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
Jim wrote:
What I really want is a Numeric array but I don't think Numeric supports
importing files.
Hmmm... Maybe the scipy package?
I think scipy.io.read_array might help, but I've never used it.
STeVe
--
http://mail.python.org/mailman/listinfo/python-list
Le Wed, 13 Apr 2005 16:46:53 +0100, Jim a écrit :
What I really want is a Numeric array but I don't think Numeric supports
importing files.
Numeric arrays can be serialized from/to files through pickles :
import Numeric as N
help(N.load)
help(N.dump)
(and it is space efficient)
Jim
--
Jim wrote:
Hi all
Is this the best way to preallocate a list of integers?
listName = range(0,length)
For serious numerical work you should use Numeric or Numarray, as
others suggested. When I do allocate lists the initial values 0:n-1 are
rarely what I want, so I use
ivec = n*[None]
so that
F. Petitjean wrote:
Le Wed, 13 Apr 2005 16:46:53 +0100, Jim a écrit :
What I really want is a Numeric array but I don't think Numeric supports
importing files.
Numeric arrays can be serialized from/to files through pickles :
import Numeric as N
help(N.load)
help(N.dump)
(and it is space
Steven Bethard wrote:
Jim wrote:
What I really want is a Numeric array but I don't think Numeric
supports importing files.
Hmmm... Maybe the scipy package?
I think scipy.io.read_array might help, but I've never used it.
STeVe
Sounds promising.
I only got Numeric because I wanted scipy but I've
ivec = n*[None]
so that if I use a list element before intializing it, for example
ivec[0] += 1
I get an error message
File xxnone.py, line 2, in ?
ivec[0] += 1
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'int'
This is in the same spirit as Python's (welcome) termination
Bill Mill [EMAIL PROTECTED] writes:
Bill Mill [EMAIL PROTECTED] writes:
I would profile your app to see that it's your append which is taking
ages, but to preallocate a list of strings would look like:
[This is an average length string for i in range(approx_length)]
I don't think there's
On Wed, 13 Apr 2005 14:28:51 +0100, Jim [EMAIL PROTECTED]
wrote:
Thanks for the suggestions. I guess I must ensure that this is my bottle
neck.
code
def readFactorsIntoList(self,filename,numberLoads):
1. numberLoads is not used.
factors = []
f = open(self.basedir +
21 matches
Mail list logo