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


Groups > comp.lang.python > #72327

Re: Yet another "simple" headscratcher

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gary.herron@islandtraining.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'newbie': 0.05; 'problem:': 0.07; '[0]': 0.09; 'append': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'def': 0.12; '0],': 0.16; 'hint': 0.16; 'subject:simple': 0.16; 'wrote:': 0.18; '(the': 0.22; '>>>': 0.22; 'example': 0.22; 'header:User-Agent:1': 0.23; 'received:emailsrvr.com': 0.24; 'looks': 0.24; 'received:(smtp server)': 0.26; 'references': 0.26; 'header:In-Reply-To:1': 0.27; 'gary': 0.31; 'josh': 0.31; 'common': 0.35; 'created': 0.35; 'but': 0.35; 'there': 0.35; 'data,': 0.36; 'being': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'rather': 0.38; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'simple': 0.61; 'demonstrates': 0.84; 'mistake': 0.91
X-Virus-Scanned OK
Date Fri, 30 May 2014 20:57:32 -0700
From Gary Herron <gary.herron@islandtraining.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Yet another "simple" headscratcher
References <a7dbde77-6bec-4b04-a940-5e8beb926232@googlegroups.com>
In-Reply-To <a7dbde77-6bec-4b04-a940-5e8beb926232@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10503.1401508915.18130.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1401508915 news.xs4all.nl 2854 [2001:888:2000:d::a6]:52166
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72327

Show key headers only | View raw


On 05/30/2014 08:38 PM, Josh English wrote:
> ...
>
> def zero_matrix(rows, cols):
>      row = [0] * cols
>      data = []
>      for r in range(rows):
>          data.append(row)
>
>      return Matrix(data)

There is a simple and common newbie mistake here.    It looks like you 
are appending several copies of a zero row to data, but in fact you are 
appending multiple references to a single row.  (The hint is that you 
only created *one* row.)

Put the
    row = [0] * cols
inside the loop so each append is using its own row rather than one 
shared row being used multiple times.


Here's a small example that demonstrates problem:

 >>> row = [0,0,0,0]
 >>> data = []
 >>> data.append(row)
 >>> data.append(row)
 >>> data[0][0] = 99
 >>> data
[[99, 0, 0, 0], [99, 0, 0, 0]]


Gary Herron


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