我有一个字符串列表,我想知道列表中最长字符串的长度。有什么简单的方法吗?
更广泛地说,我通常想知道数据帧中列的最长字符串的长度。我只需要了解数据是什么样的,所以我希望有一个像df['column'].maxlength这样的方便的方法,而不是去for loop获取数字。
发布于 2014-04-05 07:29:05
下面是比较:
#!/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是最快的
输出:
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发布于 2014-04-05 06:51:17
def longest_string(string_list):
return max(len(s) for s in string_list)示例运行:
l = ['abc', 'de', 'longest']
longest_string(l)
# 7发布于 2014-04-05 06:55:52
def longest(strings): return max(strings, key=len)您可以找到内置的max函数这里的文档。键用于从每个字符串中提取一个值。
https://stackoverflow.com/questions/22877592
复制相似问题