简体   繁体   English

定义嵌套列表 Function 用于计算 Python 中列表和元素的数量

[英]Defining a Nested List Function for Counting Numbers of Lists and Elements in Python

I am trying to define a function that takes nested lists and outputs:我正在尝试定义一个采用嵌套列表和输出的 function:

(1) How many lists are in the list, (1) 列表中有多少个列表,

and (2) Whether the number of elements in each list are the same. (2)每个列表的元素个数是否相同。

I have two nested lists:我有两个嵌套列表:

nl1: [[1, 2, 3, 4, 5], [3, 4, 5, 6, 7], [2, 4, 6, 8, 10]] nl1: [[1, 2, 3, 4, 5], [3, 4, 5, 6, 7], [2, 4, 6, 8, 10]]

nl2: [[1, 2, 3, 4, 5], [3, 4, 6, 7], [2, 4, 6, 8, 10]] nl2: [[1, 2, 3, 4, 5], [3, 4, 6, 7], [2, 4, 6, 8, 10]]

the function name is nlc() nested list count function 名称是 nlc() 嵌套列表计数

nl1 = [[1, 2, 3, 4, 5], [3, 4, 5, 6, 7], [2, 4, 6, 8, 10]]

nl2 = [[1, 2, 3, 4, 5], [3, 4, 6, 7], [2, 4, 6, 8, 10]]

def nlc(n):

    sl = len(n)

    print("Number of Lists is", sl)

    for list in n:
        r = list(map(len, n))
        if r ==list()
        print("Lengths Match")
        else print("Lengths Not Equal; Check Lists")

Two things:两件事情:

(P1) Python keeps returning an error saying that r = list(map(len, n)) is wrong because it is a string. (P1) Python 不断返回一个错误,指出 r = list(map(len, n)) 是错误的,因为它是一个字符串。

(P2) I can't seem to figure out how to write the code that checks whether each nested list has the same number of elements. (P2) 我似乎不知道如何编写代码来检查每个嵌套列表是否具有相同数量的元素。

Moreover, when I test P1, it runs just fine:此外,当我测试 P1 时,它运行得很好:

nl1 = [[1, 2, 3, 4, 5], [3, 4, 5, 6, 7], [2, 4, 6, 8, 10]]

r = list(map(len, nl1))

print(r)

So I am not sure what's happening to the argument with I am defining the function.所以我不确定我正在定义 function 的论点发生了什么。

I suppose you are using list() built-in method and also using it as a variable in a loop, this is causing an error.我想您正在使用 list() 内置方法并将其用作循环中的变量,这会导致错误。 You can do the same task as this您可以执行与此相同的任务

#Function definition
def nlc(n):
    '''It checks how many lists are in the list, and whether the number of elements in each list are the same.'''
    sl = len(n)
    print("Number of Lists is", sl)
    lengths = []
    for element in n: 
        lengths.append(len(element))          #appending length of each sublist
    if len(set(lengths)) == 1:                #checking if all elements are same in a list. It means that all lengths are equal
        print("Lengths Match")
    else:
        print("Lengths Not Equal; Check Lists")

nl1 = [[1, 2, 3, 4, 5], [3, 4, 5, 6, 7], [2, 4, 6, 8, 10]]
nl2 = [[1, 2, 3, 4, 5], [3, 4, 6, 7], [2, 4, 6, 8, 10]]
nlc(nl2)

you can try this also你也可以试试这个

nl1 = [[1, 2, 3, 4, 5], [3, 4, 5, 6, 7], [2, 4, 6, 8, 10]]
nl2 = [[1, 2, 3, 4, 5], [3, 4, 6, 7], [2, 4, 6, 8, 10]]

def nlc(n):
    
    sl = len(n)
    print(f"Number of Lists is {sl}")
    
    r = set(map(len, n))
    if len(r) == 1:
        print("Lengths Match")
    else:
        print("Lengths Not Equal; Check Lists")

nlc(nl1)

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

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