Parsing info into a 2d list
Bengt Richter
bokr at oz.net
Thu Oct 21 00:19:00 EDT 2004
On Thu, 21 Oct 2004 01:41:32 GMT, bokr at oz.net (Bengt Richter) wrote:
[...]
>----< slope.py >------------------------
[...]
> def __init__(self, lines):
> lines = iter(lines)
> for name in 'ncols nrows xllcorner yllcorner cellsize'.split():
> setattr(self, name, int(lines.next().split()[1]))
> self.NoData = lines.next().split()[1]
> mx = []
> for r in xrange(self.nrows):
> mx.append([float(v) for v in lines.next().split()])
> list.__init__(self, mx)
> def __str__(self):
> return '\n'.join(['<Slopes object:']+map(str, list(self))+['>'])
>
I don't like that useless separate mx. Below seems better, since apparently the base class
supplies an initialized empty list (though I'm not sure exactly how that is implemented):
def __init__(self, lines):
lines = iter(lines)
for name in 'ncols nrows xllcorner yllcorner cellsize'.split():
setattr(self, name, int(lines.next().split()[1]))
self.NoData = lines.next().split()[1]
for r in xrange(self.nrows):
self.append([float(v) for v in lines.next().split()])
Always room for improvement ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list