首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取列表(python)中最长字符串的长度?

如何获取列表(python)中最长字符串的长度?
EN

Stack Overflow用户
提问于 2014-04-05 06:47:43
回答 4查看 2.9K关注 0票数 1

我有一个字符串列表,我想知道列表中最长字符串的长度。有什么简单的方法吗?

更广泛地说,我通常想知道数据帧中列的最长字符串的长度。我只需要了解数据是什么样的,所以我希望有一个像df['column'].maxlength这样的方便的方法,而不是去for loop获取数字。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-04-05 07:29:05

下面是比较:

代码语言:javascript
复制
#!/usr/bin/python

import cProfile
from timeit import Timer
from faker import Faker

def longest1(lists):
    return max(len(s) for s in lists)

def longest2(lists):
    return len(max(lists, key=len))

def longest3(lists):
    return len(sorted(lists, key=len)[-1])

s = Faker()
seq = [ s.word() for x in range(100) ]

func = [ longest1, longest2, longest3 ]

for f in func:
    t = Timer(lambda: f(seq))
    print f.__name__, cProfile.run('t.timeit(number=1000)')

结果: longest2是最快的

输出:

代码语言:javascript
复制
longest1          204011 function calls in 0.046 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.046    0.046 <string>:1(<module>)
     1000    0.000    0.000    0.045    0.000 long.py:22(<lambda>)
     1000    0.001    0.000    0.045    0.000 long.py:7(longest1)
   101000    0.025    0.000    0.031    0.000 long.py:8(<genexpr>)
        1    0.000    0.000    0.000    0.000 timeit.py:143(setup)
        1    0.000    0.000    0.046    0.046 timeit.py:178(timeit)
        1    0.000    0.000    0.046    0.046 timeit.py:96(inner)
        1    0.000    0.000    0.000    0.000 {gc.disable}
        1    0.000    0.000    0.000    0.000 {gc.enable}
        1    0.000    0.000    0.000    0.000 {gc.isenabled}
        1    0.000    0.000    0.000    0.000 {globals}
   100000    0.007    0.000    0.007    0.000 {len}
     1000    0.013    0.000    0.044    0.000 {max}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        2    0.000    0.000    0.000    0.000 {time.time}


None
longest2          4011 function calls in 0.011 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.011    0.011 <string>:1(<module>)
     1000    0.001    0.000    0.010    0.000 long.py:10(longest2)
     1000    0.000    0.000    0.011    0.000 long.py:22(<lambda>)
        1    0.000    0.000    0.000    0.000 timeit.py:143(setup)
        1    0.000    0.000    0.011    0.011 timeit.py:178(timeit)
        1    0.000    0.000    0.011    0.011 timeit.py:96(inner)
        1    0.000    0.000    0.000    0.000 {gc.disable}
        1    0.000    0.000    0.000    0.000 {gc.enable}
        1    0.000    0.000    0.000    0.000 {gc.isenabled}
        1    0.000    0.000    0.000    0.000 {globals}
     1000    0.000    0.000    0.000    0.000 {len}
     1000    0.010    0.000    0.010    0.000 {max}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        2    0.000    0.000    0.000    0.000 {time.time}


None
longest3          4011 function calls in 0.031 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.031    0.031 <string>:1(<module>)
     1000    0.001    0.000    0.031    0.000 long.py:13(longest3)
     1000    0.000    0.000    0.031    0.000 long.py:22(<lambda>)
        1    0.000    0.000    0.000    0.000 timeit.py:143(setup)
        1    0.000    0.000    0.031    0.031 timeit.py:178(timeit)
        1    0.000    0.000    0.031    0.031 timeit.py:96(inner)
        1    0.000    0.000    0.000    0.000 {gc.disable}
        1    0.000    0.000    0.000    0.000 {gc.enable}
        1    0.000    0.000    0.000    0.000 {gc.isenabled}
        1    0.000    0.000    0.000    0.000 {globals}
     1000    0.000    0.000    0.000    0.000 {len}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
     1000    0.029    0.000    0.029    0.000 {sorted}
        2    0.000    0.000    0.000    0.000 {time.time}


None
票数 6
EN

Stack Overflow用户

发布于 2014-04-05 06:51:17

代码语言:javascript
复制
def longest_string(string_list):
    return max(len(s) for s in string_list)

示例运行:

代码语言:javascript
复制
l = ['abc', 'de', 'longest']
longest_string(l)
# 7
票数 2
EN

Stack Overflow用户

发布于 2014-04-05 06:55:52

代码语言:javascript
复制
def longest(strings): return max(strings, key=len)

您可以找到内置的max函数这里的文档。键用于从每个字符串中提取一个值。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22877592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档