Prev: Using site-packages with alt-installed Python version
Next: Global variables for python applications
From: Terry Reedy on 16 May 2010 15:22 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 lists is perhaps easier and more efficient. And you can iterate and be guaranteed to get the sublists in the expected order.
From: Gregory Ewing on 17 May 2010 03:39 > 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 range(3)]) matrix[2, 1] += 1 matrix[-1, 2] += 1 -- Greg
First
|
Prev
|
Pages: 1 2 Prev: Using site-packages with alt-installed Python version Next: Global variables for python applications |