This shows a fairly high-performance (although slower than initializing to exact size) way to fill in an array of unknown size in numpy:
data = numpy.zeros( (1, 1) )
N = 0
while True:
row = ...
if not row: break
# assume every row has shape (K,)
K = row.shape[0]
if (N >= data.shape[0]):
# over-expand: any ratio around 1.5-2 should produce good behavior
data.resize( (N*2, K) )
if (K >= data.shape[1]):
# no need to over-expand: presumably less common
data.resize( (N, K+1) )
# add row to data
data[N, 0:K] = row
# slice to size of actual data
data = data[:N, :]
Adapting to your case:
if count > temp.shape[0]:
temp.resize( (max( temp.shape[0]*2, count+1 ), temp.shape[1]) )
if i > temp.shape[1]:
temp.resize( (temp.shape[0], max(temp.shape[1]*2, i+1)) )
# now safe to use temp[count, i]
You may also want to keep track of the actual data sizes (max count, max i) and trim the array later.
Answer from Alex I on Stack OverflowThis shows a fairly high-performance (although slower than initializing to exact size) way to fill in an array of unknown size in numpy:
data = numpy.zeros( (1, 1) )
N = 0
while True:
row = ...
if not row: break
# assume every row has shape (K,)
K = row.shape[0]
if (N >= data.shape[0]):
# over-expand: any ratio around 1.5-2 should produce good behavior
data.resize( (N*2, K) )
if (K >= data.shape[1]):
# no need to over-expand: presumably less common
data.resize( (N, K+1) )
# add row to data
data[N, 0:K] = row
# slice to size of actual data
data = data[:N, :]
Adapting to your case:
if count > temp.shape[0]:
temp.resize( (max( temp.shape[0]*2, count+1 ), temp.shape[1]) )
if i > temp.shape[1]:
temp.resize( (temp.shape[0], max(temp.shape[1]*2, i+1)) )
# now safe to use temp[count, i]
You may also want to keep track of the actual data sizes (max count, max i) and trim the array later.
Given your follow-up comment, it sounds like you are trying to do something like the following:
arr1 = { 'sensor1' : ' ', 'sensor2' : ' ', 'sensor_n' : ' ' } #dictionary of sensors (a blank associative array)
#take not of the curly braces '{ }'
#inside the braces are key : value pairs
arr1['sensor1'] = 23
arr1['sensor2'] = 55
arr1['sensor_n'] = 125
print arr1
for k,v in arr1.iteritems():
print k,v
for i in arr1:
print arr1[i]
The Python Tutorials on Dictionaries should be able to give you the insight you are seeking.
You can use numpy:
import numpy as np
my_array = np.zeros([height, width])
For example:
>>> np.zeros([3, 5])
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])
Note that if you specifically need integers, you should use the dtype argument
>>> np.zeros([3, 5], dtype=int)
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
The numpy equivalent of the code you posted is:
import numpy as np
my_array = np.empty([height, width])
empty reserves uninitialized space, so it is slightly faster than zeros, because zeros has to make sure that everything is initially set to 0.
Note that, like in C/C++, you should write values before reading them, because they initially "contain garbage".
Docs:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.empty.html
https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html
You're technically trying to index an uninitialized array. You have to first initialize the outer list with lists before adding items; Python calls this "list comprehension".
# Creates a list containing 5 lists, each of 8 items, all set to 0
w, h = 8, 5
Matrix = [[0 for x in range(w)] for y in range(h)]
#You can now add items to the list:
Matrix[0][0] = 1
Matrix[6][0] = 3 # error! range...
Matrix[0][6] = 3 # valid
Note that the matrix is "y" address major, in other words, the "y index" comes before the "x index".
print Matrix[0][0] # prints 1
x, y = 0, 6
print Matrix[x][y] # prints 3; be careful with indexing!
Although you can name them as you wish, I look at it this way to avoid some confusion that could arise with the indexing, if you use "x" for both the inner and outer lists, and want a non-square Matrix.
If you really want a matrix, you might be better off using numpy. Matrix operations in numpy most often use an array type with two dimensions. There are many ways to create a new array; one of the most useful is the zeros function, which takes a shape parameter and returns an array of the given shape, with the values initialized to zero:
>>> import numpy
>>> numpy.zeros((5, 5))
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])
Here are some other ways to create 2-d arrays and matrices (with output removed for compactness):
numpy.arange(25).reshape((5, 5)) # create a 1-d range and reshape
numpy.array(range(25)).reshape((5, 5)) # pass a Python range and reshape
numpy.array([5] * 25).reshape((5, 5)) # pass a Python list and reshape
numpy.empty((5, 5)) # allocate, but don't initialize
numpy.ones((5, 5)) # initialize with ones
numpy provides a matrix type as well, but it is no longer recommended for any use, and may be removed from numpy in the future.
Build a Python list and convert that to a Numpy array. That takes amortized O(1) time per append + O(n) for the conversion to array, for a total of O(n).
Use with AI a = []
for x in y:
a.append(x)
a = np.array(a)
You can do this:
Use with AIa = np.array([])
for x in y:
a = np.append(a, x)