Re: Updating values in a dictionary

2010-05-17 Thread Gregory Ewing
On 5/16/2010 1:36 PM, Thomas wrote: row = dict([(x,0) for x in range(3)]) matrix = dict([(x,row) for x in range(-3,4,1)]) matrix[2][1] += 1 matrix[-1][2] += 1 Another possibility is to use a single dict with tuples for the indexes. matrix = dict([((x, y), 0) for x in range(-3, 4) for y in r

Re: Updating values in a dictionary

2010-05-16 Thread Terry Reedy
On 5/16/2010 1:36 PM, Thomas wrote: Greetings I am having a darn awful time trying to update a matrix: row = dict([(x,0) for x in range(3)]) matrix = dict([(x,row) for x in range(-3,4,1)]) matrix[2][1] += 1 matrix[-1][2] += 1 Dicts are fine for sparse matrixes, but when filled in, a list of

Re: Updating values in a dictionary

2010-05-16 Thread Thomas
Chris Wow, that was a very fast response. Thank you, it works (of course)... Cheers "Chris Rebert" wrote in message news:mailman.264.1274032106.32709.python-l...@python.org... On Sun, May 16, 2010 at 10:36 AM, Thomas wrote: Greetings I am having a darn awful time trying to update a matri

Re: Updating values in a dictionary

2010-05-16 Thread Peter Otten
Chris Rebert wrote: > Nested comprehensions may be hard to understand, so you may wish to > write it using a function instead: > > def make_row(): > return dict([(x,0) for x in range(3)]) > > matrix = dict([(x,make_row()) for x in range(-3,4,1)]) Another way to skin the cat: >>> row = dict.fro

Re: Updating values in a dictionary

2010-05-16 Thread superpollo
Thomas ha scritto: Greetings I am having a darn awful time trying to update a matrix: row = dict([(x,0) for x in range(3)]) matrix = dict([(x,row) for x in range(-3,4,1)]) matrix[2][1] += 1 matrix[-1][2] += 1 """ Got: a 1 in all col 1 and 2 {-3: {0: 0, 1: 1, 2: 1}, -2: {0: 0, 1: 1, 2: 1}, -

Re: Updating values in a dictionary

2010-05-16 Thread Chris Rebert
On Sun, May 16, 2010 at 10:36 AM, Thomas wrote: > Greetings > > I am having a darn awful time trying to update a matrix: > > row = dict([(x,0) for x in range(3)]) > matrix = dict([(x,row) for x in range(-3,4,1)]) All the columns refer to the very same row dict (`row` obviously). Python doesn't do

Updating values in a dictionary

2010-05-16 Thread Thomas
Greetings I am having a darn awful time trying to update a matrix: row = dict([(x,0) for x in range(3)]) matrix = dict([(x,row) for x in range(-3,4,1)]) matrix[2][1] += 1 matrix[-1][2] += 1 """ Got: a 1 in all col 1 and 2 {-3: {0: 0, 1: 1, 2: 1}, -2: {0: 0, 1: 1, 2: 1}, -1: {0: 0, 1: 1, 2: 1}