Best way to initialize and fill an numpy array?

前端 未结 5 1788
闹比i
闹比i 2020-12-29 03:10

I want to initialize and fill a numpy array. What is the best way?

This works as I expect:

>>> import numpy as np
>>>          


        
5条回答
  •  情歌与酒
    2020-12-29 03:55

    You could also try:

    In [79]: np.full(3, np.nan)
    Out[79]: array([ nan,  nan,  nan])
    

    The pertinent doc:

    Definition: np.full(shape, fill_value, dtype=None, order='C')
    Docstring:
    Return a new array of given shape and type, filled with `fill_value`.
    

    Although I think this might be only available in numpy 1.8+

提交回复
热议问题