Reputation: 191
I have an array phase
[ (3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0)
(3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0)
(3.0598295600822953, 0.376861295611, 0.314535588286, -0.041169303628, 0.0)]
and I would like to resize with the method resize
phase.resize(MAXLINE)
and I get this result
[ (3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0)
(3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0)
(3.0598295600822953, 0.376861295611, 0.314535588286, -0.041169303628, 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.0, 0.0, 0.0, 0.0)]
I would like to know if it's possible to set a specific value (Nan or -99999) instead of the default value 0.0
Upvotes: 3
Views: 19322
Reputation: 10417
If you are not attached to np.resize()
you can do it this way:
import numpy as np
old_array = [ (3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0),
(3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0),
(3.0598295600822953, 0.376861295611, 0.314535588286, -0.041169303628, 0.0)]
maxline = 20
# If you want fullfil extra dimension with NaN
arr = [(np.NAN,)*len(old_array[0])]*(maxline - len(old_array))
# If you want fullfil extra dimension with anything else
# arr = np.array([(ANYTHING_YOU_WANT,)*len(old_array[0])]*(maxline - old_array.size))
new_ = old_array + arr
print numpy.array(new_)
>>
[(3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0), (3
.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0), (3.059
8295600822953, 0.376861295611, 0.314535588286, -0.041169303628, 0.0), (nan, nan,
nan, nan, nan), (nan, nan, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, nan
, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, na
n, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, n
an, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, nan, nan, nan, nan), (nan,
nan, nan, nan, nan), (nan, nan, nan, nan, nan), (nan, nan, nan, nan, nan), (nan,
nan, nan, nan, nan), (nan, nan, nan, nan, nan)]
Upvotes: 1
Reputation: 15180
Making the assumption that you want your array to have shape (MAXLINE,5)
and that your array is a 2-dimensional array and not a list of tuples (as the format in your question seems to suggest), this would work:
import numpy as np
MAXLINE = 4
a=np.array([ [3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0],
[3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0],
[3.0598295600822953, 0.376861295611, 0.314535588286, -0.041169303628, 0.0]])
np.append(a,np.ones((MAXLINE-a.shape[0],a.shape[1]))*np.NAN,axis=0)
produces:
array([[ 3.05354009, 0.3713459 , 0.31295379, -0.01252314, 0. ],
[ 3.05668483, 0.37397185, 0.31368239, -0.02645439, 0. ],
[ 3.05982956, 0.3768613 , 0.31453559, -0.0411693 , 0. ],
[ nan, nan, nan, nan, nan]])
Explanation:
np.ones()
takes a shape parameter, so I'm adding enough rows to make the final shape (MAXLINE,5)
, where 5 is the number of columns of a
(ie: a.shape[1]
).
In np.append()
, the axis=0
parameter tells numpy to add rows. If you don't have this, it flattens the arrays.
Of course, you can replace np.NAN
with any value you prefer.
Upvotes: 2
Reputation: 4462
Because Python arrays don't might resize arrays you can use numpy or write own functions, similar below:
def resize(array, new_size, new_value=0):
"""Resize to biggest or lesser size."""
element_size = len(array[0]) #Quantity of new elements equals to quantity of first element
if new_size > len(array):
new_size = new_size - 1
while len(array)<=new_size:
n = tuple(new_value for i in range(element_size))
array.append(n)
else:
array = array[:new_size]
return array
#Test it
a = [ (3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0),
(3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0),
(3.0598295600822953, 0.376861295611, 0.314535588286, -0.041169303628, 0.0)]
a = resize(a, 5)
print a
a = resize(a, 2)
print a
a = resize(a, 3, 28)
print a
#Output:
#New size 5, default value 0
#[(3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0), (3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0), (3.0598295600822953, 0.376861295611, 0.314535588286, -0.041169303628, 0.0), (0, 0, 0, 0, 0), (0, 0, 0, 0, 0)]
#new size 2
#[(3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0), (3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0)]
#New size 4, default value 28
#[(3.0535400914168154, 0.371345899229, 0.312953794281, -0.0125231427371, 0.0), (3.056684825749555, 0.373971853521, 0.313682391117, -0.0264543909236, 0.0), (28, 28, 28, 28, 28)]
Upvotes: 1