Python sequences and scalars behave differently from numpy 1d arrays and scalars. Let's run through a few basic examples to get the hang of it (or skip to bottom if in a hurry):
# define our test objects
a_range = range(2,6)
a_list = list(a_range)
a_array = np.array(a_list)
# ranges don't add
a_range+a_range
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: unsupported operand type(s) for +: 'range' and 'range'
# lists do add but with different semantics ...
a_list+a_list
# [2, 3, 4, 5, 2, 3, 4, 5]
# ... from numpy arrays
a_array+a_array
# array([ 4, 6, 8, 10])
# what happens if we mix types?
# range and list don't mix:
a_range+a_list
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: unsupported operand type(s) for +: 'range' and 'list'
# but as soon as there is a numpy object involved it "wins":
a_range+a_array
# array([ 4, 6, 8, 10])
a_list+a_array
# array([ 4, 6, 8, 10])
# How about scalars?
py_scalar = 3
np_scalar = np.int64(py_scalar)
# again, in pure python you cannot add something to a range ...
a_range+py_scalar
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: unsupported operand type(s) for +: 'range' and 'int'
# or a list
a_list+py_scalar
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: can only concatenate list (not "int") to list
# but you can add a Python or numpy scalar to a numpy object
a_array+py_scalar
# array([5, 6, 7, 8])
a_array+np_scalar
# array([5, 6, 7, 8])
# Now if the scalar is a numpy object, again, it "wins":
a_range+np_scalar
# array([5, 6, 7, 8])
a_list+np_scalar
# array([5, 6, 7, 8])
So to summarize, Python sequences and numpy 1d arrays have different semantics, in particular, with binary operators like "+" or "-" or "*". If at least one operand is a numpy object (array or scalar) numpy wins: Non numpy objects will be converted (flat sequences become 1d arrays) and numpy semantics will apply.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…