擅长:python、mysql、java
<p>这就是如何将数组中的所有值从<i>float</i>更改为<i>int</i>:</p>
<pre><code># Example array
arr_float = [3.45, 5.6, 3.89, 10.1]
# to integer values
arr_int = [int(n) for n in arr_float] # this is a list comprehension
arr_int
>>> [3, 5, 3, 10] # float values to integer get always rounded down to whole number
</code></pre>
<p>或者,将该数组设置为numpy数组,并按照您的问题中的方式进行操作:</p>
<pre><code>import numpy as np
# Example array
arr_float = [3.45, 5.6, 3.89, 10.1]
# to numpy array
arr_np = np.array(arr_float)
arr_np.astype(int)
>>> array([ 3, 5, 3, 10])
</code></pre>
<p>这是一个可以在需要更改时调用的函数:</p>
<pre><code># Function changing float values to integer values
def to_int(input_array):
return [int(n) for n in input_array]
# Example array
arr_float = [3.86, 2.9999, 9.46, 3.00013, 8.56]
to_int(arr_float)
>>> [3, 2, 9, 3, 8]
</code></pre>