【发布时间】:2018-08-11 19:28:56
【问题描述】:
我对 Python 比较陌生,经常看到类似的例子:
def max_wordnum(texts):
count = 0
for text in texts:
if len(text.split()) > count:
count = len(text.split())
return count
重复的len(text.split()) 是否被Python 中的解释器/编译器以某种方式优化掉了,还是只需要将len(text.split()) 存储在变量中的CPU 周期的两倍?
【问题讨论】:
-
不,不是。通话将进行两次。所以,还有优化代码的空间。
-
很好的例子是这样写函数:
max(len(text.split()) for text in texts)
标签: python