How to solve invalid index to scalar variable

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…

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

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » How to solve invalid index to scalar variable." Deven | Sciencx - Sunday March 14, 2021, https://www.scien.cx/2021/03/14/how-to-solve-invalid-index-to-scalar-variable/
HARVARD
Deven | Sciencx Sunday March 14, 2021 » How to solve invalid index to scalar variable., viewed ,<https://www.scien.cx/2021/03/14/how-to-solve-invalid-index-to-scalar-variable/>
VANCOUVER
Deven | Sciencx - » How to solve invalid index to scalar variable. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/14/how-to-solve-invalid-index-to-scalar-variable/
CHICAGO
" » How to solve invalid index to scalar variable." Deven | Sciencx - Accessed . https://www.scien.cx/2021/03/14/how-to-solve-invalid-index-to-scalar-variable/
IEEE
" » How to solve invalid index to scalar variable." Deven | Sciencx [Online]. Available: https://www.scien.cx/2021/03/14/how-to-solve-invalid-index-to-scalar-variable/. [Accessed: ]
rf:citation
» How to solve invalid index to scalar variable | Deven | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.