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
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
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
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
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},
-
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
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}