简体   繁体   中英

Python find maximum string length in the list of lists of lists

big_list = [[['asdf','ad'],['aqwe','rt']],['lkjyui','op'],['dfgh','hjk']]

Goal is to find the maximum string length in the whole big_list

My approach:

big_list = [[['asdf','ad'],['aqwe','rt']],['lkjyui','op'],['dfgh','hjk']]

listdf= pd.concat(pd.DataFrame(item).T for item in big_list ).reset_index(drop=True)
listdf = 
       0     1
0    asdf  aqwe
1      ad      
2  lkjyui    op
3    dfgh   hjk

print(listdf.astype(str).applymap(lambda x: len(x)).max().max())
6

Is there a better way of doing it?

With pandas:

out = listdf.stack().str.len().max()

In pure python:

out = max(len(x) for l in big_list for x in l)

Output: 6

If uisng pandas then

one = list(pandas.core.common.flatten(big_list))
print(len(max(one, key=len)))

give #

6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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