2D array

Adam DePrince adam at cognitcorp.com
Wed Dec 8 16:14:16 EST 2004


On Wed, 2004-12-08 at 15:06, Steven Bethard wrote:
> 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:

The use of  None as the default parameter was on purpose; the lack of
"magic" in python is often cited in religious wars between python and
perl aficionados.  Use of get(something, None) was on purpose, the level
of familiarity with the language implied by the original question
suggested that the notion of optional parameters, and specifically those
of get, may not have been immediately obvious.

As for a[0,0] instead of a[(0,0)] ... the former just *looks* so
aesthetically wrong to me that I've never used it, and had forgotten
that it was even possible.   

> 
>  >>> print a.get((5, 2))
> None
> 
> Steve
Adam DePrince 





More information about the Python-list mailing list