0

I currently have the following (generalized) code, where I am trying to increase counter if list_1[list_2[i]] is equal to 3. I want list_1[list_2[i]] to return the element at index list_2[i] in list_1. However, list_2 is full of lists which contain coordinates, like (4,7), meaning that when I run this code, I get the following error:

TypeError: list indices must be integers or slices, not list

My goal is to have list_1[list_2[i]] return something like (0, 0), so that my if statement becomes if array[0, 0] == 3:

import numpy as np

for item in list_1:
    i = 0
    counter = 0

    while i < len(item):
        if array[list_1[list_2[i]]] == 3:
            counter += 1

To put it simply (since I realize how poor an explanation above is), if I have list_1 with multiple parameters of its own, how can I treat its contents literally, such that list_2d[list_1] becomes list_2d[1, 2], without unpacking list_1 into separate variables?

list = [1, 2]
list_2d = [[0, 1, 2], [3, 4, 5]]

print(list_2d[list])

Hope for output:

5

Actual output:

TypeError: list indices must be integers or slices, not list

0 Answers0