两层嵌套list,选取list中最长的list的长度

时间:2023-03-09 03:08:52
两层嵌套list,选取list中最长的list的长度
 # coding=utf-8

 import pandas as pd

 file_path = "test_aa.xlsx"
dt = pd.read_excel(file_path)
data = dt['con']
locdata = []
for i in data:
locdata.append(str(i).split(",")) print(locdata) #change to [[1,2,3],[1,2,3]]
length = []
for i in locdata:
length.append(len(i))#计算长度并存储
print(length)
print(length[length.index(max(length))])#length.index(max(length)读取最大值的位置,然后再定位取出最大值

输出:也就是想计算第一行中,list里面最长的list的长度是多少。

[['', '', ''], ['', '', ''], ['', ''], ['', '', ''], ['', '', ''], ['', '', ''], ['', ''], ['', '', ''], ['', '', '', '', '']]
[3, 3, 2, 3, 3, 3, 2, 3, 5]
5

test_aa.xlsx如下:

name    con
T1 2,3,5
T2 1,2,4
T3 3,5
T5 2,3,4
T1 2,3,5
T2 1,2,4
T3 3,5
T5 2,3,4
T5 1,2,3,4,5

有没有更好的方式,希望可以帮助到我。