Skip to main content

1st case :

  >>> import numpy as np
  >>> x=np.array(0)
  >>> x=np.append(x,1)
  >>> x
  array([0, 1])

xx contains 2 elements. Why is that ?!

2nd case :

>>> x=np.array([])
>>> x=np.append(x,1)
>>> x
array([ 1.])

xx contains 1 element, as expected.

What's the difference between np.array(0)np.array(0) and np.array([])np.array([]) ?

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([]) ?

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([]) ?

Source Link
dpeng
  • 507
  • 2
  • 7
  • 18

Numpy append to an empty array

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([]) ?