Updating values in a dictionary

Gregory Ewing greg.ewing at canterbury.ac.nz
Mon May 17 03:39:40 EDT 2010


> 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



More information about the Python-list mailing list