2D array

Steven Bethard steven.bethard at gmail.com
Wed Dec 8 15:06:56 EST 2004


Adam DePrince wrote:
> If your data is sparse you might want to consider using a dictionary
> where the key is a tuple representing the coordinates.
> 
> a = {}
> a[(0,0)] = 0
> a[(0,1)] = 1
[snip]
>>>>print a.get( (5,0), None )

Good point.  Note that you don't need the parentheses in the assignments 
or item accesses:

 >>> a = {}
 >>> a[0,0] = 10
 >>> a[0,0]
10

Also note that you don't need to specify None as the default value when 
you call dict.get -- None is assumed if no default value is supplied:

 >>> print a.get((5, 2))
None

Steve



More information about the Python-list mailing list