列表方法之间有什么区别 append()
和 extend()
?
列表方法之间有什么区别 append()
和 extend()
?
append
:在最后追加对象。
x = [1, 2, 3]
x.append([4, 5])
print (x)
给你: [1, 2, 3, [4, 5]]
extend
:通过附加迭代中的元素来扩展列表。
x = [1, 2, 3]
x.extend([4, 5])
print (x)
给你: [1, 2, 3, 4, 5]
append
向列表添加元素,和 extend
将第一个列表与另一个列表(或另一个可迭代列表,不一定是列表)连接起来。
>>> li = ['a', 'b', 'mpilgrim', 'z', 'example']
>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li.append("new")
>>> li
['a', 'b', 'mpilgrim', 'z', 'example', 'new']
>>> li.insert(2, "new")
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']
>>> li.extend(["two", "elements"])
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
从 潜入Python。
列表方法append和extend之间有什么区别?
append
将其参数作为单个元素添加到列表的末尾。列表本身的长度将增加一个。extend
迭代其参数,将每个元素添加到列表中,扩展列表。但是,可迭代参数中的许多元素会增加列表的长度。append
该 list.append
方法将对象附加到列表的末尾。
my_list.append(object)
无论对象是什么,无论是数字,字符串,其他列表还是其他东西,都会被添加到结尾处 my_list
作为列表中的单个条目。
>>> my_list
['foo', 'bar']
>>> my_list.append('baz')
>>> my_list
['foo', 'bar', 'baz']
所以请记住,列表是一个对象。如果将另一个列表附加到列表中,则第一个列表将是列表末尾的单个对象(可能不是您想要的):
>>> another_list = [1, 2, 3]
>>> my_list.append(another_list)
>>> my_list
['foo', 'bar', 'baz', [1, 2, 3]]
#^^^^^^^^^--- single item on end of list.
extend
该 list.extend
方法通过附加迭代中的元素来扩展列表:
my_list.extend(iterable)
因此,使用extend,iterable的每个元素都会附加到列表中。例如:
>>> my_list
['foo', 'bar']
>>> another_list = [1, 2, 3]
>>> my_list.extend(another_list)
>>> my_list
['foo', 'bar', 1, 2, 3]
请记住,字符串是可迭代的,因此如果使用字符串扩展列表,则在迭代字符串时会附加每个字符(可能不是您想要的):
>>> my_list.extend('baz')
>>> my_list
['foo', 'bar', 1, 2, 3, 'b', 'a', 'z']
__add__
,((+
)和 __iadd__
(+=
)都 +
和 +=
运营商的定义 list
。它们在语义上类似于extend。
my_list + another_list
在内存中创建第三个列表,因此您可以返回它的结果,但它要求第二个iterable是一个列表。
my_list += another_list
在原地修改列表(它 是 就地运算符,列表是可变对象,正如我们所见,因此它不会创建新列表。它也像extend一样工作,因为第二个iterable可以是任何类型的iterable。
不要混淆 - my_list = my_list + another_list
不等于 +=
- 它为您提供了分配给my_list的全新列表。
追加有 恒定的时间复杂性,O(1)。
扩展具有时间复杂度O(k)。
通过多次调用迭代 append
增加了复杂性,使其等同于extend的复杂性,并且由于extend的迭代是在C中实现的,如果您打算将迭代中的连续项追加到列表中,它总是会更快。
你可能想知道什么是更高效的,因为append可以用来实现与extend相同的结果。以下函数执行相同的操作:
def append(alist, iterable):
for item in iterable:
alist.append(item)
def extend(alist, iterable):
alist.extend(iterable)
让我们时间吧:
import timeit
>>> min(timeit.repeat(lambda: append([], "abcdefghijklmnopqrstuvwxyz")))
2.867846965789795
>>> min(timeit.repeat(lambda: extend([], "abcdefghijklmnopqrstuvwxyz")))
0.8060121536254883
评论者说:
完美的答案,我只是错过了比较只添加一个元素的时机
做语义正确的事情。如果要将所有元素附加到iterable中,请使用 extend
。如果您只是添加一个元素,请使用 append
。
好的,让我们创建一个实验,看看它是如何及时解决的:
def append_one(a_list, element):
a_list.append(element)
def extend_one(a_list, element):
"""creating a new list is semantically the most direct
way to create an iterable to give to extend"""
a_list.extend([element])
import timeit
而且我们看到,为了使用扩展而创建一个可迭代的方法是一种(轻微的)浪费时间:
>>> min(timeit.repeat(lambda: append_one([], 0)))
0.2082819009956438
>>> min(timeit.repeat(lambda: extend_one([], 0)))
0.2397019260097295
我们从中了解到,使用中没有任何好处 extend
当我们只有 一 要追加的元素。
而且,这些时间并不重要。我只是向他们展示了这一点,在Python中,做语义正确的事情是做事情 对 办法。
可以想象,您可以测试两个类似操作的时序并获得模糊或反向结果。只关注做语义正确的事情。
我们看到了 extend
在语义上更清晰,它可以运行得更快 append
, 当您打算将迭代中的每个元素追加到列表中时。
如果您只有一个元素(不是可迭代的)要添加到列表中,请使用 append
。
append
附加单个元素。 extend
附加元素列表。
请注意,如果您传递列表以追加,它仍会添加一个元素:
>>> a = [1, 2, 3]
>>> a.append([4, 5, 6])
>>> a
[1, 2, 3, [4, 5, 6]]
以下两个片段在语义上是等效的:
for item in iterator:
a_list.append(item)
和
a_list.extend(iterator)
后者可能更快,因为循环在C中实现。
您可以使用“+”来返回扩展,而不是在适当的位置扩展。
l1=range(10)
l1+[11]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]
l2=range(10,1,-1)
l1+l2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2]
同样 +=
对于现场行为,但略有不同 append
& extend
。最大的差异之一 +=
从 append
和 extend
是在功能范围中使用时,请参阅 这篇博文。
append()方法将单个项添加到列表的末尾。
x = [1, 2, 3]
x.append([4, 5])
x.append('abc')
print(x)
# gives you
[1, 2, 3, [4, 5], 'abc']
extend()方法接受一个参数,一个列表,并将参数的每个项追加到原始列表中。 (列表实现为类。“创建”列表实际上是实例化类。因此,列表具有对其进行操作的方法。)
x = [1, 2, 3]
x.extend([4, 5])
x.extend('abc')
print(x)
# gives you
[1, 2, 3, 4, 5, 'a', 'b', 'c']
从 潜入Python。