From charlesreid1

No edit summary
No edit summary
Line 6: Line 6:


<source lang="python">
<source lang="python">
# You can initialize an array with []:
In [62]: x=[]
In [62]: x=[]


Line 12: Line 14:
In [64]: x
In [64]: x
Out[64]: array([], dtype=float64)
Out[64]: array([], dtype=float64)
# to be more direct about it,


In [66]: x = np.array([],dtype=np.float64)
In [66]: x = np.array([],dtype=np.float64)
Line 17: Line 21:
In [67]: x
In [67]: x
Out[67]: array([], dtype=float64)
Out[67]: array([], dtype=float64)
# Now you can append to the empty array
In [71]: x = append(x,1)
In [72]: x = append(x,2)
In [73]: x=append(x,3)
In [74]: x
Out[74]: array([ 1.,  2.,  3.])
</source>
</source>

Revision as of 19:28, 28 July 2013

Arrays

Initializing an empty array

Sometimes you want to initalize an empty array, which is a really inefficient use of memory for large matrices but you could care less if it's a dozen items.

# You can initialize an array with []: 

In [62]: x=[]

In [63]: x=np.array(x)

In [64]: x
Out[64]: array([], dtype=float64)

# to be more direct about it,

In [66]: x = np.array([],dtype=np.float64)

In [67]: x
Out[67]: array([], dtype=float64)

# Now you can append to the empty array

In [71]: x = append(x,1)

In [72]: x = append(x,2)

In [73]: x=append(x,3)

In [74]: x
Out[74]: array([ 1.,  2.,  3.])