更加pythonic / fast append或+ = [] [英] which is more pythonic/faster append or +=[]

查看:80
本文介绍了更加pythonic / fast append或+ = []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

达到同样效果的两种方式

l + = [n]





l。追加(n)

哪个更pythonic /更快?


-

alfz1

two ways of achieving the same effect
l+=[n]

or

l.append(n)
so which is more pythonic/faster?

--
alfz1

推荐答案

alf< ask @mewrote:
alf <ask@mewrote:

实现相同效果的两种方式


l + = [n]





l.append(n)


所以哪个更pythonic /更快?
two ways of achieving the same effect
l+=[n]

or

l.append(n)
so which is more pythonic/faster?



..支持 - 也很容易衡量:


脑:~alex

..append - easy to measure, too:

brain:~ alex


python -mtimeit''L = range(3); n = 23''''x = L [:]; x.append(n)''

1000000循环,最好3:每循环使用1.31

脑:~alex
python -mtimeit ''L=range(3); n=23'' ''x=L[:]; x.append(n)''
1000000 loops, best of 3: 1.31 usec per loop

brain:~ alex

python -mtimeit''L = range(3); n = 23''''x = L [:]; x + = [n]''

1000000循环,最佳3:1.52 usec每循环

Alex
python -mtimeit ''L=range(3); n=23'' ''x=L[:]; x+=[n]''
1000000 loops, best of 3: 1.52 usec per loop
Alex


这篇关于更加pythonic / fast append或+ = []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆