在 Python 中,“.append()"和“.append()"有什么区别?和“+= []"? [英] In Python, what is the difference between ".append()" and "+= []"?

查看:25
本文介绍了在 Python 中,“.append()"和“.append()"有什么区别?和“+= []"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别:

some_list1 = []some_list1.append("某事")

some_list2 = []some_list2 += ["某事"]

解决方案

对于您的情况,唯一的区别是性能:追加的速度是原来的两倍.

Python 3.0(r30:67507,2008 年 12 月 3 日,20:14:27)[MSC v.1500 32 位(英特尔)] 在 win32 上输入帮助"、版权"、信用"或许可证"以获取更多信息.>>>导入时间>>>timeit.Timer('s.append("something")', 's = []').timeit()0.20177424499999999>>>timeit.Timer('s += ["something"]', 's = []').timeit()0.41192320500000079Python 2.5.1(r251:54863,2007 年 4 月 18 日,08:51:08)[MSC v.1310 32 位(英特尔)] 在 win32 上输入帮助"、版权"、信用"或许可证"以获取更多信息.>>>导入时间>>>timeit.Timer('s.append("something")', 's = []').timeit()0.23079359499999999>>>timeit.Timer('s += ["something"]', 's = []').timeit()0.44208112500000141

在一般情况下,append 将向列表添加一项,而 += 将复制右侧列表的所有元素进入左侧列表.

更新:性能分析

比较字节码我们可以假设 append 版本在 LOAD_ATTR + CALL_FUNCTION 和 += version -- 在 BUILD_LIST 中浪费周期.显然 BUILD_LISTLOAD_ATTR + CALL_FUNCTION 更重要.

<预><代码>>>>导入文件>>>dis.dis(compile("s = []; s.append('垃圾邮件')", '', 'exec'))1 0 BUILD_LIST 03 STORE_NAME 0 (s)6 LOAD_NAME 0 (s)9 LOAD_ATTR 1(附加)12 LOAD_CONST 0 ('垃圾邮件')15 CALL_FUNCTION 118 POP_TOP19 LOAD_CONST 1(无)22 返回值>>>dis.dis(compile("s = []; s += ['垃圾邮件']", '', 'exec'))1 0 BUILD_LIST 03 STORE_NAME 0 (s)6 LOAD_NAME 0 (s)9 LOAD_CONST 0 ('垃圾邮件')12 BUILD_LIST 115 INPLACE_ADD16 STORE_NAME 0 (s)19 LOAD_CONST 1(无)22 返回值

我们可以通过去除 LOAD_ATTR 开销来进一步提高性能:

<预><代码>>>>timeit.Timer('a("something")', 's = []; a = s.append').timeit()0.15924410999923566

What is the difference between:

some_list1 = []
some_list1.append("something")

and

some_list2 = []
some_list2 += ["something"]

解决方案

For your case the only difference is performance: append is twice as fast.

Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141

In general case append will add one item to the list, while += will copy all elements of right-hand-side list into the left-hand-side list.

Update: perf analysis

Comparing bytecodes we can assume that append version wastes cycles in LOAD_ATTR + CALL_FUNCTION, and += version -- in BUILD_LIST. Apparently BUILD_LIST outweighs LOAD_ATTR + CALL_FUNCTION.

>>> import dis
>>> dis.dis(compile("s = []; s.append('spam')", '', 'exec'))
  1           0 BUILD_LIST               0
              3 STORE_NAME               0 (s)
              6 LOAD_NAME                0 (s)
              9 LOAD_ATTR                1 (append)
             12 LOAD_CONST               0 ('spam')
             15 CALL_FUNCTION            1
             18 POP_TOP
             19 LOAD_CONST               1 (None)
             22 RETURN_VALUE
>>> dis.dis(compile("s = []; s += ['spam']", '', 'exec'))
  1           0 BUILD_LIST               0
              3 STORE_NAME               0 (s)
              6 LOAD_NAME                0 (s)
              9 LOAD_CONST               0 ('spam')
             12 BUILD_LIST               1
             15 INPLACE_ADD
             16 STORE_NAME               0 (s)
             19 LOAD_CONST               1 (None)
             22 RETURN_VALUE

We can improve performance even more by removing LOAD_ATTR overhead:

>>> timeit.Timer('a("something")', 's = []; a = s.append').timeit()
0.15924410999923566

这篇关于在 Python 中,“.append()"和“.append()"有什么区别?和“+= []"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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