nested lists as arrays
bruno modulix
onurb at xiludom.gro
Mon Feb 14 07:52:11 EST 2005
benjamin.cordes at blawrc.de wrote:
> Hi,
>
> why can't I do this:
>
> dummy = self.elements[toy][tox]
>
> self.elements[toy][tox] = self.elements[fromy][fromx]
> self.elements[fromy][fromx] = dummy
>
> after initialising my nested list like this:
>
> self.elements = [[0 for column in range(dim)] for row in
> range(dim) ]
>
Sorry, I'm not psychic enough to guess what is exactly your problem:
- what do you mean "can't do" ? You have a traceback ? please post it.
You have unexpected results ? please describe.
- what are self, dim, toy, tox, fromy, fromx ?
- is all that code in the same method ?
- etc etc
So please post more informations if you expect us to help you.
Note that the following code is correct:
>>> l = [[0 for i in range(3)] for y in range(3)]
>>> l
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> l[0][0] = 1
>>> l
[[1, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> l[0][0], l[0][1] = l[0][1], l[0][0]
>>> l
[[0, 1, 0], [0, 0, 0], [0, 0, 0]]
>>>
So I guess that your problem has nothing to do with nested lists.
(Also note BTW that, thanks to the magic of multiple assignement, you
don't need the dummy stuff. The pythonic idiom for swapping 2 bindings is
a, b = b, a)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list