Python:比较不同列表的长度并返回最长的

时间:2019-09-02 11:21:55

标签: python-2.7 list compare

我想比较5个列表的长度,并返回最长的列表。

我不知道... 也许是这样的:

a = [1,1,1,1]
b = [1]
c = [1,1,1]
d = [1,1,1,1,1]
e = [1,1,1,1,1]

L = [a,b,c,d,e]

def compare(lists):
    counter = count()
    previous = lists[0]
    group_index = next(counter)
    for value in lists:
        if len(value) >= len(previous):
                ...

结果应为“ e”。

1 个答案:

答案 0 :(得分:0)

下面是py3

a = [1,1,1,1]
b = [1]
c = [1,1,1]
d = [1,1,1,1,1]
e = [1,1,1,1,2]
f = [1,1,1,1,1,2]
L = [a,f,b,c,d,e]

def compare(lists):
    previous = lists[0]
    for value in lists:
        if len(value) >= len(previous):
            previous=value
    return previous
Rsss=compare(L)
print(Rsss)
print([ k for k,v in locals().items() if v == Rsss][0])

python 2将项目更改为迭代项

print()进行打印