我有一个看起来像这样的代码:
def somefunction(somelist):
for item in somelist:
if len(item) > 10:
do something
elif len(item) > 6:
do something
elif len(item) > 3:
do something
else:
do something
由于我多次调用 len(item) ,这样做是否效率低下?最好按如下方式编写代码,还是它们的性能完全相同?
def somefunction(somelist):
for item in somelist:
x = len(item)
if x > 10:
do something
elif x > 6:
do something
elif x > 3:
do something
else:
do something