How to fix IndexError: invalid index to scalar variable #108944
-
Select Topic AreaQuestion BodyThis code generates error:
at the line: How to fix it?
testcv is:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You are trying to index into a scalar (non-iterable) value:
When you call [y for y in test] you are iterating over the values already, so you get a single value in y. Your code is the same as trying to do the following:
I'm not sure what you're trying to get into your results array, but you need to get rid of [y[1] for y in y_test]. If you want to append each y in y_test to results, you'll need to expand your list comprehension out further to something like this:
|
Beta Was this translation helpful? Give feedback.
You are trying to index into a scalar (non-iterable) value:
When you call [y for y in test] you are iterating over the values already, so you get a single value in y.
Your code is the same as trying to do the following:
I'm not sure what you're trying to get into your results array, but you need to get rid of [y[1] for y in y_test].
If you want to append each y in y_test to results, you'll need to expand your list comprehension out further to something like this:
[results.append(..., y) for y in y_test]
Or just use a for loop: