array = some kind of list with 3 columns and unlimited amount of rows with data inside of it.
Volume = array[0][2]
counter = 0
for i in array:
if Volume == array[i][2]: #<------ why is this line a problem?
counter += 1
The Python "TypeError: list indices must be integers or slices, not float" occurs when we use a floating-point number to access a list at a specific index. To solve the error, convert the float to an integer, e.g. my_list[int(my_float)] .
The Python "TypeError: list indices must be integers or slices, not tuple" occurs when we pass a tuple between the square brackets when accessing a list at index. To solve the error, make sure to separate nested list elements with commas and correct the index accessor.
You cannot access a value in a string using another string. To solve TypeError: string indices must be integers; we should reference our dictionary instead of “ele“. We can access elements in our dictionary using a string. This is because dictionary keys can be strings.
To find the index of an element in a list, you use the index() function. It returns 3 as expected. However, if you attempt to find an element that doesn't exist in the list using the index() function, you'll get an error.
This is a classic mistake. i
in your case is already an element from array
(i.e. another list), not an index of array
(not an int
), so
if Volume == i[2]:
counter += 1
You can check the Python tutorial. Also, try doing this:
for i in array:
print (i)
And see what you get!
Also I would advise to stick to naming conventions: variables are normally lower-case (volume
, not Volume
). In this case i
is misleading. row
or elem
would be much more suitable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With