Monday, 14 January 2008

array in python

Two types of array:

1. the array comes with Python.
import array
a=array.array('f',[1,2,3])
aa=a*2 # got array('f',[1.0,2.0,3.0,1.0,2.0,3.0])
print a #not suitable for numerical calculation,http://docs.python.org/lib/module-array.html

2.Numpy (third party modules, need to download from http://numpy.scipy.org
from numpy import *
b=
array([1,2,3])
bb=b*2 # got array([2, 4, 6])

P.S.: the two types can exist simultaneously, but when they are calculated together, the 'array.array' will be converted to 'numpy.array' automatically.
>>> import numpy
>>> a=numpy.array([1,2,3])
>>> import array
>>> b=array.array('f',[0.1,0.1,0.1])
>>> print a
[1 2 3]
>>> print b
array('f', [0.10000000149011612, 0.10000000149011612, 0.10000000149011612])
>>> c=a+b
>>> print c
[ 1.1 2.1 3.1]

>>> a
array([1, 2, 3])
>>> b
array('f', [0.10000000149011612, 0.10000000149011612, 0.10000000149011612])
>>> c
array([ 1.1, 2.1, 3.1])
>>>

No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter