Python的列表方法append和extend之间有什么区别?

2024-04-20 02:18:40 发布

您现在位置:Python中文网/ 问答频道 /正文

列表方法append()extend()之间有什么区别


Tags: 方法列表append区别extend
3条回答

What is the difference between the list methods append and extend?

  • append将其参数作为单个元素添加到列表的末尾。列表本身的长度将增加1
  • extend迭代其参数,将每个元素添加到列表中,扩展列表。列表的长度将增加iterable参数中的元素数量

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 at the end of the list.

extend

list.extend方法通过从iterable中添加元素来扩展列表:

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]

请记住,字符串是一个iterable,因此,如果使用字符串扩展列表,则在迭代字符串时会追加每个字符(这可能不是您想要的):

>>> 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并不等同于+=-它为您提供了一个分配给我的列表的全新列表

时间复杂性

追加has(amortizedconstant time complexity,O(1)

Extend具有时间复杂度O(k)

通过对append的多次调用进行迭代增加了复杂性,使其与extend相当,而且由于extend的迭代是用C实现的,因此如果您打算将iterable中的连续项追加到列表中,那么它总是会更快

关于list object implementation source中的“摊销”:

    /* This over-allocates proportional to the list size, making room
     * for additional growth.  The over-allocation is mild, but is
     * enough to give linear-time amortized behavior over a long
     * sequence of appends() in the presence of a poorly-performing
     * system realloc().

这意味着我们可以提前获得比所需内存更大的重新分配带来的好处,但我们可以在下一次边际重新分配时用更大的内存来支付。所有附加的总时间在O(n)处是线性的,每个附加分配的时间变成O(1)

演出

您可能想知道什么性能更好,因为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

发表关于时间安排的评论

有评论说,

Perfect answer, I just miss the timing of comparing adding only one element

做语义正确的事情。如果要在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

我们发现,特意创建一个iterable只是为了使用extend是一种(轻微的)时间浪费:

>>> min(timeit.repeat(lambda: append_one([], 0)))
0.2082819009956438
>>> min(timeit.repeat(lambda: extend_one([], 0)))
0.2397019260097295

我们从中了解到,当我们只有一个元素要追加时,使用extend没有任何好处

而且,这些时间安排也不那么重要。我只是向他们展示,在Python中,做语义正确的事情就是用正确的方式做事情™.

可以想象,您可能会在两个可比较的操作上测试计时,并得到一个不明确或相反的结果。只需专注于做语义正确的事情

结论

我们看到extend在语义上更清晰,当您打算将iterable中的每个元素追加到列表时,它可以比append运行得更快

如果只有一个元素(不在iterable中)要添加到列表中,请使用append

append将一个元素添加到列表中,并且extend将第一个列表与另一个列表(或另一个iterable,不一定是列表)连接起来

>>> li = ['a', 'b', 'mpilgrim', 'z', 'example']
>>> li
['a', 'b', 'mpilgrim', 'z', 'example']

>>> li.append("new")
>>> li
['a', 'b', 'mpilgrim', 'z', 'example', 'new']

>>> li.append(["new", 2])
>>> li
['a', 'b', 'mpilgrim', 'z', 'example', 'new', ['new', 2]]

>>> li.insert(2, "new")
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', ['new', 2]]

>>> li.extend(["two", "elements"])
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', ['new', 2], 'two', 'elements']

^{}:在末尾追加对象

x = [1, 2, 3]
x.append([4, 5])
print(x)

给您:[1, 2, 3, [4, 5]]


^{}:通过从iterable中添加元素来扩展列表

x = [1, 2, 3]
x.extend([4, 5])
print(x)

给你:[1, 2, 3, 4, 5]

相关问题 更多 >