When I try to assign a string to an array like this:
CoverageACol[0,0] = "Hello"
I get the following error
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
CoverageACol[0,0] = "hello"
ValueError: setting an array element with a sequence.
However, assigning an integer does not result in an error:
CoverageACol[0,0] = 42
CoverageACol is a numpy array.
Please help! Thanks!
You get the error because NumPy’s array is homogeneous, meaning it is a multidimensional table of elements all of the same type. This is different from a multidimensional list-of-lists in "regular" Python, where you can have objects of different type in a list.
Regular Python:
NumPy:
So, it depends on what you want to achieve, why do you want to store a string in an array filled for the rest with numbers? If that really is what you want, you can set the datatype of the NumPy array to string:
Notice that only the first letter of
Hellogets assigned. If you want the whole word to get assigned, you need to set an array-protocol type string: