Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72328

Re: Yet another "simple" headscratcher

References <a7dbde77-6bec-4b04-a940-5e8beb926232@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2014-05-30 21:54 -0600
Subject Re: Yet another "simple" headscratcher
Newsgroups comp.lang.python
Message-ID <mailman.10504.1401508949.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, May 30, 2014 at 9:38 PM, Josh English
<Joshua.R.English@gmail.com> wrote:
> I am trying to whip up a quick matrix class that can handle multiplication.
>
> Should be no problem, except when it fails.
>
> [SNIP]
>
> def zero_matrix(rows, cols):
>     row = [0] * cols
>     data = []
>     for r in range(rows):
>         data.append(row)

Each row of the matrix that you create here is the *same* list, not
different lists that happen to be equal.  So when you mutate one row,
you mutate all of them.  See:
https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Yet another "simple" headscratcher Josh English <Joshua.R.English@gmail.com> - 2014-05-30 20:38 -0700
  Re: Yet another "simple" headscratcher Josh English <Joshua.R.English@gmail.com> - 2014-05-30 20:55 -0700
  Re: Yet another "simple" headscratcher Gary Herron <gary.herron@islandtraining.com> - 2014-05-30 20:57 -0700
  Re: Yet another "simple" headscratcher Ian Kelly <ian.g.kelly@gmail.com> - 2014-05-30 21:54 -0600
    Re: Yet another "simple" headscratcher wxjmfauth@gmail.com - 2014-05-30 23:46 -0700

csiph-web