How do I initialize a list please?
jerf at compy.attbi.com
jerf at compy.attbi.com
Tue Dec 31 17:07:36 EST 2002
On Tue, 31 Dec 2002 13:49:58 +0000, Christopher wrote:
> Excellent point. How would you go about making a 2d array? I would
> assume that, since you are just repeating the same list data 10 times
> that you could use something like:
Since moving to OO programming in Python I have not needed
multi-dimensional arrays. Unless you really are doing some numerical
methods, in which case you probably ought to be using NumPy or something
anyhow, you usually want a single-dimensional array of some kind of
complicated object, better defined as a class.
class ComplicatedObject:
def __init__(self, start = ""):
self.data = start
def addme(self, thing):
self.data += thing
def clear(self, thing):
self.data = ""
Then create a list of those, like
l = []
for i in range(10):
l.append(ComplicatedObject())
or
l = [ComplicatedObject for x in range(10)]
The point of this is you'll find you can write much more sensible
manipulation routines that aren't mucking around with object[3][21][18]
all the time, or nested loops, or other hard-for-humans-to-follow
constructs.
Even if you really ARE dealing with straight number lists, you may *still*
be able to find a conceptualization to wrap it in an object that allows
you to manipulate it coherently, rather then directly.
More information about the Python-list
mailing list