问题
I was experimenting with numpy arrays and created a numpy array of strings:
ar1 = np.array(['avinash', 'jay'])
As I have read from from their official guide, operations on numpy array are propagated to individual elements. So I did this:
ar1 * 2
But then I get this error:
TypeError Traceback (most recent call last)
in ()
----> 1 ar1 * 2
TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'int'
But when I used dtype=object
ar1 = np.array(['avinash', 'jay'], dtype=object)
while creating the array I am able to do all operations.
Can anyone tell me why this is happening?
解决方法
NumPy arrays are stored as contiguous blocks of memory. They usually have a single datatype (e.g. integers, floats or fixed-length strings) and then the bits in memor