列表中的append,extend,+=,+的區別

先來看列表中的append和extend的幫助文檔
app


>>> help(list.append)        # 往列表末尾添加對象ide

Help on method_descriptor:函數

 

append(...)性能

    L.append(object) -> None -- append object to end對象

 

>>> help(list.extend)       # 往經過添加迭代對象來擴充列表ip

Help on method_descriptor:element

 

extend(...)文檔

    L.extend(iterable) -> None -- extend list by appending elements from the iterableit

 

>>>io

 再來看二者的示例

# append日後添加單獨一個元素

>>> A = ["a", "b"]

>>> A.append(1)

>>> A

['a', 'b']

# append日後添加一整個列表

>>> A = ["a", "b"]

>>> A.append(["c", "d"])

>>> A

['a', 'b', ['c', 'd']]

# append日後添加一個迭代對象

>>> A = ["a", "b"]

>>> A.append(range(10))

>>> A

['a', 'b', range(0, 10)]

# append日後添加函數和類對象

>>> a = [1, 2]

>>> class A:

...     pass

>>> def B():

...     pass

>>> a.append(A)

>>> a.append(B)

>>> a

[1, 2, <class '__main__.A'>, <function B at 0x000000000404B598>]

# 往列表A,擴充一個列表對象

>>> A = ["a", "b"]

>>> A.extend(["c", "d"])

>>> A

['a', 'b', 'c', 'd']

# 往列表A,擴充一個迭代對象range(4)

>>> A = ["a", "b"]

>>> A.extend(range(4))

>>> A

['a', 'b', 0, 1, 2, 3]

# 往列表A,擴充一個迭代對象"ABCD"

>>> A = [1, 2]

>>> A.extend("ABCD")

>>> A

[1, 2, 'A', 'B', 'C', 'D']

再來看往extend後添加單獨元素會報錯,由於單獨元素是不可迭代的。 


>>> A = ["a", "b"]

>>> A.extend(1)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'int' object is not iterable

 

'int' object is not iterable

綜合上面的示例及報錯信息,來總結extend, append方法:


extend只能添加可迭代對象,而append便可添加可迭代對象,也能夠添加單獨元素

append後面添加的對象,無論是單獨元素,列表,可迭代對象,通通算一個元素往列表最後加入

extend日後擴充的對象,是拿後面可迭代的全部元素往裏面添加

 再來看+,+=的示例

# +=操做

>>> A = ["a", "b"]

>>> B = ["c", "d"]

>>> A += B

>>> A

['a', 'b', 'c', 'd']

 

# +操做

>>> A = ["a", "b"]

>>> B = ["c", "d"]

>>> A = A + B

>>> A

['a', 'b', 'c', 'd']

從上面的例子能看看出不管是+仍是+=,都只能在列表之間操做


>>> A = ["a", "b"]

>>> B = 1

>>> A += B

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'int' object is not iterable

 

'int' object is not iterable

>>> A = A + B

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: can only concatenate list (not "int") to list

 

can only concatenate list (not "int") to list

>>> A + range(10)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: can only concatenate list (not "range") to list

 

can only concatenate list (not "range") to list

>>>

 綜合上面的示例及報錯,來總結+,+=:


不管是+仍是+=只能在兩個列表之間操做

都不能添加非列表對象

再來從id來看操做對象的變化

# 執行extend後,A的id不變

>>> A = [1, 2]

>>> B = [3, 4]

>>> id(A)

61099656

>>> A.extend(B)

>>> A

[1, 2, 3, 4]

>>> id(A)

61099656

 

# 執行append後,A的id不變

>>> A = [1, 2]

>>> B = [3, 4]

>>> id(A)

65986184

>>> A.append(B)

>>> A

[1, 2, [3, 4]]

>>> id(A)

65986184

 

# 執行+=後,A的id不變

>>> A = [1, 2]

>>> B = [3, 4]

>>> id(A)

63943112

>>> A += B

>>> A

[1, 2, 3, 4]

>>> id(A)

63943112

 

# 執行+後,A的id發生變化

>>> A = [1, 2]

>>> B = [3, 4]

>>> id(A)

61026568

>>> A = A + B

>>> A

[1, 2, 3, 4]

>>> id(A)

61026952

從上面的id來看,彷佛又可總結出:


執行extend,append,+=這三個操做都是原地修改列表,都不會引發id的變化

執行+,會拷貝出一個新的對象,會引發id的變化

若是執行+操做,一旦操做對象不少的話,常常須要拷貝新對象,性能是四者中最差的

最後總結:

extend原地修改列表,只可添加可迭代對象,待添加的對象有多少個就往原對象中添加多少個

append原地修改列表,可添加任何對象,但不管是什麼對象,在末尾添加,並且只算一個元素

+=原地修改列表,只可執行列表之間的操做,效果上至關於extend

+會拷貝成一個新對象,並且只能執行列表之間的操做,性能問題要考慮  

相關文章
相關標籤/搜索