python hack of the day -- "listable" functions

Travis Oliphant olipt at mayo.edu
Thu May 13 23:20:29 EDT 1999


> >
> >This isn't true -- there are languages which do exactly this.  I think
> >Icon is one, and I know APL, J, and K are

Add the Numeric extensions (umath) in particular and you get this behavior
now.
> 1) we had an aditional builtin type 'dollar', so we could write
> >>> x = $[1, 2, 3, 4]
> >>> type(x)
> <type 'dollar'>
> >>> x
> $[1, 2, 3, 4]
> >>> 2*x+1
> $[3, 5, 7, 9]
> >>> undollar (x)
> [1, 2, 3, 4]
> 

Go get the Numeric extensions,

>>> from Numeric import *
>>> x = array([1,2,3,4])
>>> 2*x + 1
>>> array([3,5,7,9])
>>> x.tolist()
>>> [1, 2, 3, 4]

You can also do some clever things with multidimensional arrays.  It's
pretty much equivalent to what's available in J (as far as I know).

But, there are no $ signs....

Travis






More information about the Python-list mailing list