Casting and module "array"

Curtis Jensen cjensen at bioeng.ucsd.edu
Tue Jan 2 13:31:01 EST 2001


"Edward C. Jones" wrote:
> 
> Is there a better way to cast arrays made from module "array"?
> Turning a large array into a list would seem to be slow.
> 
> import array
> 
> def cast(typecode, arr):
>     return array.array(typecode, list(arr))
> 
> if __name__ == '__main__':
>     Barr = array.array('B', [0, 1, 254, 255])
>     harr = cast('h', Barr)
>     print list(harr)
>     barr = cast('b', Barr)  # Fails.
> 
> I would use "astype" from Numeric (aka NumPy) instead but NumPy
> doesn't support unsigned short or unsigned long.
> 
> Thanks,
> Ed Jones

instead of using "list(arr)" you can use "arr.tolist()"  I believe that
it is pretty quick. The array from the array module is realy just a
list, tolist() Is not realy casting, it is returning the underlying data
structure.

-- 
Curtis Jensen
cjensen at bioeng.ucsd.edu
http://www-bioeng.ucsd.edu/~cjensen/
FAX (425) 740-1451



More information about the Python-list mailing list