简体   繁体   English

如何在 2D/3D 列表中查找元素的长度?

[英]How to find the length of an element in a 2D/3D list?

This function returns the maximum number of elements of a list, of a tuple, in a list.此 function 返回列表中元组的最大元素数。 I think this is a correct approach but I keep getting the error:我认为这是一种正确的方法,但我不断收到错误:

'TypeError: list indices must be integers or slices, not tuple' 'TypeError:列表索引必须是整数或切片,而不是元组'

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:这是 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预期 output:5

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

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.第一个导致错误的是for i in List将遍历list的不是整数的值。 However you put L[i] right below in which i has to be an integer.但是,您将L[i]放在下面,其中i必须是 integer。
The second one is the fact that list is a reserved name in python refering to the type list.第二个是list是 python 中的保留名称,指的是类型列表。 Thus, you should not name your list list (or a string str ) but rather L .因此,您不应将列表命名为list (或字符串str ),而应命名为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:或者您可以通过更改以下行将您的行保留在 for 循环中,如下所示:

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

It is bad practice to name variables after inbuilt python functions, as it overwrites them.在内置 python 函数之后命名变量是不好的做法,因为它会覆盖它们。 I have renamed list to lst for this answer.对于这个答案,我已将list重命名为lst

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.目前,您正在以i遍历列表中的每个元素,因此在调用lst[i][1]时,第一个i将是(0, [1, 2, 3]) ,它不是有效索引。 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:您需要将 for 循环更改为for i in range(len(lst)) ,然后您将循环遍历索引而不是元素,如下所示:

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:或者,您可以按原样使用i ,并使用它代替lst[i] ,如下所示:

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.但是,这可能无法按照您的network[i][1]线的要求工作,因此第一种方法最适合您的问题。

If you need to find the maximum length of the list then you can use this below snippet it returns the maximum length ie, 5 in the given case.如果您需要找到列表的最大长度,那么您可以使用下面的代码段,它返回最大长度,即在给定情况下为 5。 I failed to understand len(network[i][1]) part you can edit the remaining part by yourself.我无法理解len(network[i][1])部分,您可以自己编辑其余部分。 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.我认为您将len(network[i][1])分配到最大值,这就是为什么您没有得到想要的结果。 Also, one suggestion is never to use the inbuilt function name as variables.此外,一个建议是永远不要使用内置的 function 名称作为变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM