📜  Python追加、扩展和插入的区别

📅  最后修改于: 2021-09-14 02:11:23             🧑  作者: Mango

列表就像动态大小的数组,在其他语言中声明(C++ 中的向量和Java的ArrayList)。列表不必总是同质的,这使其成为Python最强大的工具。单个列表可能包含数据类型,如整数、字符串和对象。

列表是可变的,因此即使在创建之后也可以更改它们。列表有很多种方法,最常用的列表方法有append()、insert()、extend()等。在本文中,我们将讨论append()、insert() 和、extend()之间的区别)方法在Python列表中。

附加

它在列表的末尾添加一个元素。在 append函数传递的参数作为单个元素添加到列表的末尾,并且列表的长度增加 1。

句法:

list_name.append(element)

元素可以是字符串、整数、元组或其他列表。

例子:

Python3
# python program to demonstrate
# working of append function
 
# assign list
l = ['geeks']
 
# use method
l.append('for')
l.append('geeks')
 
# display list
print(l)


Python3
# python program to demonstrate
# working of insert function
 
# assign list
l = ['geeks', 'geeks']
 
# use method
l.insert(1, 'for')
 
# display list
print(l)


Python3
# python program to demonstrate
# working of extend function
 
# assign list
l = ['hello', 'welcome', 'to']
 
# use method
l.extend(['geeks', 'for', 'geeks'])
 
# display list
print(l)


Python3
# Python program to demonstrate
# comparison between the three methods
 
# assign lists
list_1 = [1, 2, 3]
list_2 = [1, 2, 3]
list_3 = [1, 2, 3]
 
a = [2, 3]
 
# use methods
list_1.append(a)
list_2.insert(3, a)
list_3.extend(a)
 
# display lists
print(list_1)
print(list_2)
print(list_3)


输出:

['geeks', 'for', 'geeks']

此方法可用于在任何所需位置插入值。它需要两个参数-元素和必须插入元素的索引。

句法:

list_name(index,element)

元素可以是字符串、对象或整数。

例子:

蟒蛇3

# python program to demonstrate
# working of insert function
 
# assign list
l = ['geeks', 'geeks']
 
# use method
l.insert(1, 'for')
 
# display list
print(l)

输出:

['geeks', 'for', 'geeks']

延长

此方法将可迭代对象(元组、字符串或列表)的每个元素附加到列表的末尾,并通过作为参数传递的可迭代对象的元素数增加列表的长度。

句法:

list_name.extend(iterable)

例子:

蟒蛇3

# python program to demonstrate
# working of extend function
 
# assign list
l = ['hello', 'welcome', 'to']
 
# use method
l.extend(['geeks', 'for', 'geeks'])
 
# display list
print(l)

输出:

['hello', 'welcome', 'to', 'geeks', 'for', 'geeks']

追加、扩展和插入之间的区别

append() insert() extend()
The element passed as an argument is appended to the end of the list The element passed as the argument can be inserted at any desired position by passing the index along with it as a parameter. Each element of the iterable passed as an argument gets appended to the end of the list.
An iterable passed as an argument appends without any change as a single element to the end of the list. An iterable passed as an argument appends without any change as a single element to the desired index of the list. An iterable passed as an argument will append each of its elements to the end of the list
Length of the list increases by 1 Length of the list increases by 1 Length of the list increases by the number of elements in the iterable.
Has a constant time complexity of O(1) Has a linear complexity of O(n). Has a time complexity of O(k) where k is the length of the iterable.

比较单个程序中的方法:

蟒蛇3

# Python program to demonstrate
# comparison between the three methods
 
# assign lists
list_1 = [1, 2, 3]
list_2 = [1, 2, 3]
list_3 = [1, 2, 3]
 
a = [2, 3]
 
# use methods
list_1.append(a)
list_2.insert(3, a)
list_3.extend(a)
 
# display lists
print(list_1)
print(list_2)
print(list_3)

输出:

[1, 2, 3, [2, 3]]
[1, 2, 3, [2, 3]]
[1, 2, 3, 2, 3]