numpy的追加到一个空数组 [英] Numpy append to an empty array
问题描述
第一种情况:
>>>导入numpy的是NP
>>> X = np.array(0)
>>> X = np.append(X,1)
>>> X
阵列([0,1])
X
包含2个元素。这是为什么?!
第二种情况:
>>> X = np.array([])
>>> X = np.append(X,1)
>>> X
阵列([1])
X
包含1元,符合市场预期。
有什么之间的 np.array(0)
和 np.array([])
?区别p>
在你的第一种情况下,要创建称为数组 X
将包含一个值,该值 0
。
在你的第二个情况下,你正在创建一个名为空数组x
将包含任何值,但仍是一个数组。
首例
所以,当你追加 X = np.append(X,1)
,该值 1
获取的附加阵列(已经包含0)的即它现在包含0和1
第二个案例
既然你空数组中没有价值,当你追加 X = np.append(X,1)
值 1
获取的追加和 X
的长度变为1(也就是说,它现在只包含1 )
P.S。我相信你可能会认为调用 X = np.array(0)
与 0
将使得空阵,它不!在Python,0仍取为一个数字和附加到该阵列。
1st case :
>>> import numpy as np
>>> x=np.array(0)
>>> x=np.append(x,1)
>>> x
array([0, 1])
x
contains 2 elements. Why is that ?!
2nd case :
>>> x=np.array([])
>>> x=np.append(x,1)
>>> x
array([ 1.])
x
contains 1 element, as expected.
What's the difference between np.array(0)
and np.array([])
?
In your first case, you are creating an array called x
that will containing one value, which is 0
.
In your second case you are creating an empty array called x
that will contain no values, but is still an array.
FIRST CASE
So when you append x = np.append(x,1)
, the value 1
get's appended to your array (which already contains 0) i.e. it now contains 0 and 1
SECOND CASE
Since you have no values in the empty array, when you append x=np.append(x,1)
the value 1
get's appended and the length of x
becomes 1 (i.e. it now contains only 1)
P.S. I believe you might have thought that calling x = np.array(0)
with the 0
would make it an empty array, it doesn't!! In Python, 0 is still taken to be a number and is appended to the array.
这篇关于numpy的追加到一个空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!