【问题标题】:Python find maximum string length in the list of lists of listsPython在list of lists的列表中查找最大字符串长度
【发布时间】:2022-12-17 22:17:23
【问题描述】:
big_list = [[['asdf','ad'],['aqwe','rt']],['lkjyui','op'],['dfgh','hjk']]

目标是在整个big_list中找到最大字符串长度

我的方法:

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

有更好的方法吗?

【问题讨论】:

  • 这个列表已经在数据框中了吗?如果没有,把它放在一个可能和一开始就做计算一样昂贵。您打算稍后将数据框用于任何事情吗?

标签: python list


【解决方案1】:

与大熊猫:

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

在纯 python 中:

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

输出:6

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 2019-02-25
    • 2011-08-31
    • 1970-01-01
    • 2016-06-19
    相关资源
    最近更新 更多