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.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']
列表方法追加和扩展之间有什么区别?
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 at the end of the list.
extend
list.extend
方法通过附加来自可迭代对象的元素来扩展列表:
my_list.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
定义。它们在语义上类似扩展。
my_list + another_list
在内存中创建第三个列表,因此您可以返回它的结果,但是它要求第二个可迭代的列表。
my_list += another_list
修改就地列表(它是就地运算符,并且列表是可变对象,如我们所见),因此不会创建新列表。它也像扩展一样工作,因为第二个可迭代对象可以是任何一种可迭代对象。
请勿混淆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
评论者说:
完美的答案,我只是错过了仅添加一个元素进行比较的时机
做语义上正确的事情。如果要以可迭代方式附加所有元素,请使用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 中实现时,后者可能会更快。
追加与扩充
![]()
使用 append,您可以附加一个将扩展列表的元素:
>>> a = [1,2]
>>> a.append(3)
>>> a
[1,2,3]
如果要扩展多个元素,则应使用 extend,因为您只能附加一个元素或一个元素列表:
>>> a.append([4,5])
>>> a
>>> [1,2,3,[4,5]]
这样您就可以获得一个嵌套列表
您可以像这样通过扩展来扩展单个元素
>>> a = [1,2]
>>> a.extend([3])
>>> a
[1,2,3]
或者,与追加不同的是,一次扩展更多元素而不将列表嵌套到原始列表中(这就是名称扩展的原因)
>>> a.extend([4,5,6])
>>> a
[1,2,3,4,5,6]
>>> x = [1,2]
>>> x.append(3)
>>> x
[1,2,3]
>>> x = [1,2]
>>> x.extend([3])
>>> x
[1,2,3,4]
添加更多元素... 结果不同
如果对多个元素使用 append,则必须将元素列表作为参数传递,您将获得 NESTED 列表!
>>> x = [1,2]
>>> x.append([3,4])
>>> x
[1,2,[3,4]]
相反,使用 extend,您将一个列表作为参数传递,但是您将获得一个列表,其中包含未嵌套在旧元素中的新元素。
>>> z = [1,2]
>>> z.extend([3,4])
>>> z
[1,2,3,4]
因此,使用更多元素,您将使用 extend 获得包含更多项目的列表。您将使用 append 将更多的元素添加到列表中,但是将一个嵌套列表的元素添加到代码输出中,您可以清楚地看到。
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 。
您可以使用 “+” 返回扩展名,而不是就地扩展名。
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(object)
- 通过将对象添加到列表来更新列表。
x = [20]
# List passed to the append(object) method is treated as a single object.
x.append([21, 22, 23])
# Hence the resultant list length will be 2
print(x)
--> [20, [21, 22, 23]]
extend(list)
- 本质上是串联两个列表。
x = [20]
# The parameter passed to extend(list) method is treated as a list.
# Eventually it is two lists being concatenated.
x.extend([21, 22, 23])
# Here the resultant list's length is 4
print(x)
[20, 21, 22, 23]
extend()
可以与迭代器参数一起使用。这是一个例子。您希望通过以下方式从列表列表中列出一个列表:
从
list2d = [[1,2,3],[4,5,6], [7], [8,9]]
你要
>>>
[1, 2, 3, 4, 5, 6, 7, 8, 9]
您可以使用itertools.chain.from_iterable()
进行操作。该方法的输出是一个迭代器。它的实现等效于
def from_iterable(iterables):
# chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
for it in iterables:
for element in it:
yield element
回到我们的例子,我们可以做
import itertools
list2d = [[1,2,3],[4,5,6], [7], [8,9]]
merged = list(itertools.chain.from_iterable(list2d))
并获得通缉名单。
这是等效地extend()
与迭代器参数一起使用的方式:
merged = []
merged.extend(itertools.chain.from_iterable(list2d))
print(merged)
>>>
[1, 2, 3, 4, 5, 6, 7, 8, 9]