This content originally appeared on CodeSource.io and was authored by Deven
In this article, you will learn how to solve invalid index to scalar variable error in Python.
Let’s look at a code example that produces the same error.
import numpy as np
x = np.array([[3, 4], [5, 6], [7, 8]])
print(x[0][0][1])
Output:
Traceback (most recent call last):
File "<string>", line 3, in <module>
IndexError: invalid index to scalar variable.
In order to solve invalid index to scalar variable error you need to check if the index is wrong, such as using the original two-dimensional array, using a three-tier index. Consider the code example below:
import numpy as np
x = np.array([[3, 4], [5, 6], [7, 8]])
print(x[0],x[1],x[2])
output
[3 4] [5 6] [7 8]
The post How to solve invalid index to scalar variable appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Deven

Deven | Sciencx (2021-03-14T09:38:25+00:00) How to solve invalid index to scalar variable. Retrieved from https://www.scien.cx/2021/03/14/how-to-solve-invalid-index-to-scalar-variable/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.