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

 1 # coding=utf-8
 2 
 3 
 4 import pandas as pd
 5 
 6 file_path = "test_aa.xlsx"
 7 dt = pd.read_excel(file_path)
 8 data = dt['con']
 9 locdata = []
10 for i in data:
11     locdata.append(str(i).split(","))
12 
13 print(locdata)  #change to [[1,2,3],[1,2,3]]
14 length = []
15 for i in locdata:
16     length.append(len(i))#计算长度并存储
17 print(length)
18 print(length[length.index(max(length))])#length.index(max(length)读取最大值的位置,然后再定位取出最大值

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

[['2', '3', '5'], ['1', '2', '4'], ['3', '5'], ['2', '3', '4'], ['2', '3', '5'], ['1', '2', '4'], ['3', '5'], ['2', '3', '4'], ['1', '2', '3', '4', '5']]   
[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

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

posted @ 2018-01-08 16:32  shizhenqiang  阅读(1462)  评论(0编辑  收藏  举报