1
yu = np.array([np.nan, np.nan, np.nan, np.nan, np.nan,])

rte = np.array([1,2,3,4,5])

yu[0] = rte[0]
yu # array([  1.,  nan,  nan,  nan,  nan])

yet..

[yu[i] = rte[i] for i in range(len(rte))]

SyntaxError: invalid syntax

Specifically, I'm trying to fill the nan in an array with other array of same length:

[pred[first_c_rowNA, 0::][0::, wNA[0]][i] = output[i] for i in np.arange(len(output))]

SyntaxError: invalid syntax

pred[first_c_rowNA, 0::][0::, wNA[0]] # array([ nan,  nan,  nan, ...,  nan,  nan,  nan])

and

output # array([ 0.,  0.,  0., ...,  0.,  0.,  0.]) # not all are zeros

Thanks

4

3 回答 3

3

Why not something simply like:

>>> import numpy as np
>>> rte = np.array([1,2,3,4,5])
>>> yu = np.array([np.nan, np.nan, np.nan, np.nan, np.nan,])
>>> yu[:] = rte

>>> yu
array([ 1.,  2.,  3.,  4.,  5.])

Or if you need a nan mask:

yu[np.isnan(yu)] = values

For example:

>>> yu
array([ 0.20087116,         nan,  0.71742786,  0.05037165,  0.25646742,
               nan,  0.27702335,         nan,         nan,  0.62272575])

>>> yu[np.isnan(yu)] = np.random.rand(4)

>>> yu
array([ 0.20087116,  0.6701011 ,  0.71742786,  0.05037165,  0.25646742,
        0.63462273,  0.27702335,  0.01248758,  0.61178318,  0.62272575])
于 2013-10-27T14:38:37.613 回答
1

List comprehension does not assign anything, it creates a new list, which you may then use to fill a numpy array. But most of the times when working with numpy arrays it is best to use slices:

>>> import numpy as np
>>> yu = np.array([np.nan, np.nan, np.nan, np.nan, np.nan,])
>>> rte = np.array([1,2,3,4,5])
>>> yu[:] = rte
>>> yu
array([ 1.,  2.,  3.,  4.,  5.])
于 2013-10-27T14:38:35.080 回答
0

Why don't you use:

for i in range(len(rte)):
    yu[i] = rte[i]
于 2013-10-27T15:01:16.687 回答