nested lists as arrays

benjamin.cordes at blawrc.de benjamin.cordes at blawrc.de
Mon Feb 14 12:06:30 EST 2005


Diez B. Roggisch wrote:
> 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) ]
>
> Works for me:
>
> dim = 10
> elements = [[0 for column in xrange(dim)] for row in
> xrange(dim) ]
>
> toy, tox = (2,5)
> fromy, fromx = (7,5)
>
> dummy =elements[toy][tox]
> elements[toy][tox] = elements[fromy][fromx]
> elements[fromy][fromx] = dummy
>
>
> And use xrange instead of range.
> --
> Regards,
>
> Diez B. Roggisch

thanks so far, and sorry for posting the topic now 3 times.




More information about the Python-list mailing list