3

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

4
  • 4
    Python arrays do not have a resize method, and neither does lists. Are you perhaps using Numpy or some similar library? If so, please edit your question to include that. Commented May 28, 2015 at 10:30
  • As Emil says, explain if you're using a library and ideally add it in your tags too so it shows up in searches. Commented May 28, 2015 at 10:32
  • lists have an extend method that might suit your needs. Commented May 28, 2015 at 10:40
  • @Emil Vikström, yes i am using numpy Commented May 28, 2015 at 11:00

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)]

Comments

1

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)]

1 Comment

,I ve tried your code and i get this error : p=np.concatenate(phase,arr) TypeError: only length-1 arrays can be converted to Python scalars

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.