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: 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 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: In-Reply-To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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