简体   繁体   中英

Set numpy.ndarray values to a scalar

I´ve an variable that´sa numpy array:

(Pdb) type(inFile.z)
<type 'numpy.ndarray'>
(Pdb) 
(Pdb) print inFile.z
[-0.188 -0.109 -0.02  ...,  0.373  0.356  0.385]
(Pdb)

Id´like to set all values to 20. I tried:

inFile.z[:]=20
inFile.z.fill(20)

But it doesn´t works, that is, the values reamains the same.

How can I do that?

Fill works. Give more background on what inFile is and/or try making sure you're libs are up to date.

In [3]: x = np.array([1,2,3,4,5])

In [4]: x
Out[4]: array([1, 2, 3, 4, 5])

In [5]: type(x)
Out[5]: numpy.ndarray

In [6]: x.f
x.fill     x.flags    x.flat     x.flatten

In [6]: x.fill('boo')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-b4528b13e992> in <module>()
----> 1 x.fill('boo')

ValueError: invalid literal for long() with base 10: 'boo'

In [7]: x.fill('1')

In [8]: x
Out[8]: array([1, 1, 1, 1, 1])

In [10]: x.fill(2.5342)

In [11]: x
Out[11]: array([2, 2, 2, 2, 2])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM