Home > Enterprise >  How to find the length of an element in a 2D/3D list?
How to find the length of an element in a 2D/3D list?

Time:11-23

This function returns the maximum number of elements of a list, of a tuple, in a list. I think this is a correct approach but I keep getting the error:

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

Here's the list:

list = [(0, [1, 2, 3]), (1, [0, 4, 6, 7, 9]), (2, [0, 3, 6, 8, 9]), (3, [0, 2, 8, 9]), (4, [1, 6, 7, 8]),
(5, [9]), (6, [1, 2, 4, 8]), (7, [1, 4, 8]), (8, [2, 3, 4, 6, 7]), (9, [1, 2, 3, 5])]

Here's the function:

def max_inner_list_length(list):

    maximum = 0

    for i in list:
        if len(list[i][1]) > maximum:
            maximum = len(network[i][1])

    return maximum

Example:

max_inner_list_length(list)

Expected output: 5

Actual output: TypeError: list indices must be integers or slices, not tuple

CodePudding user response:

If you need to find the maximum length of the list then you can use this below snippet it returns the maximum length i.e., 5 in the given case. I failed to understand len(network[i][1]) part you can edit the remaining part by yourself. If you need further help feel free to comment.

def max_inner_list_length(list):
   maximum = 0
   for i in range(len(list)):
      if len(list[i][1]) > maximum:
         maximum = len(list[i][1])       
return maximum
print(max_inner_list_length(list))

I think you are assigning that len(network[i][1]) to the maximum that is why you are not getting the desired result. Also, one suggestion is never to use the inbuilt function name as variables.

CodePudding user response:

There are two issues in your code.
The first one inducing the error is that for i in List will iterate through the values of list which aren't integers. However you put L[i] right below in which i has to be an integer.
The second one is the fact that list is a reserved name in python refering to the type list. Thus, you should not name your list list (or a string str) but rather L.

You should replace your code by :

def max_inner_list_length(L):
    
    maximum = 0
    for i in range(len(L)):
        if len(L[i][1]) > maximum:
            maximum = len(L[i][1]) #I suppose network is a variable
                                   #outside the function context,
    return maximum                 #which should be replace by L

Or you can keep your line in the for loop by changing the following line, like so:

def max_inner_list_length(L):
    
    maximum = 0
    for i in L:
        if len(i[1]) > maximum:
            maximum = len(i[1])
    return maximum

CodePudding user response:

It is bad practice to name variables after inbuilt python functions, as it overwrites them. I have renamed list to lst for this answer.

Currently, you are iterating through every element in the list as i, so when calling lst[i][1], the first i will be (0, [1, 2, 3]) which isn't a valid index. You can check this by running:

for i in lst:
    print(i)

You will need to change your for loop to be for i in range(len(lst)), then you will be looping through the indexes instead of the elements, as below:

def max_inner_list_length(lst):

    maximum = 0

    for i in range(len(lst)):
        if len(lst[i][1]) > maximum:
            maximum = len(network[i][1])

    return maximum

Alternatively, you can use i as is, and use that instead of lst[i], as below:

for i in range(len(lst)):
    if len(i[1]) > maximum:

However this probably won't work as required for your network[i][1] line, so the first approach is best for your problem.

  • Related