首页 > 编程知识 正文

Python 中append、extend和insert的区别

时间:2023-11-26 13:13:12 阅读:309377 作者:KWGR

List: 就像一个动态大小的数组,用另一种编程语言声明,比如 C++中的 vector 或者 Java 中的 Arraylist。列表没有必要是同构的,这是它成为 Python 中最强大工具的主要原因。单个列表可以包含不同的数据类型,如字符串、整数和对象。

正如我们所知,列表是可变的,因此即使在创建列表之后,它们也可以被更改。Lists 有几种方法,其中最常见的是追加()、插入()、扩展()。

在本教程中,我们将学习 append()、expend()和 insert()函数在 Python 的列表中是如何相互不同的。

追加()函数

append()函数用于在列表末尾添加一个元素。我们在 append()函数中传递的参数作为单个元素添加到列表的末尾,列表的长度将增加 1。

语法


list_name1.append(element_1)

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

示例:


list_1 = ['The', 'list_1']

# Using the method
list_1.append('is')
list_1.append('an')
list_1.append('example')

# Displaying the list
print ("The added elements in the given list are: ", list_1)

输出:

The added elements in the given list are: ['The', 'list_1', 'is', 'an', 'example']

插入()函数

insert() 功能用于在列表中的任意位置插入值。我们必须传递两个参数;第一个是索引,我们希望在其中插入元素,第二个是要插入的元素。

语法


list_name1.insert(index, element_1)

“element_1”可以是整数、元组、字符串或对象。

示例:


list_1 = ['The', 'is']

# Using the method
list_1.insert(3,'an')
list_1.insert(1, 'list_1')
list_1.insert(4, 'example')

# Displaying the list
print ("The inserted elements in the given list are: ", list_1)

输出:

The inserted elements in the given list are:  ['The', 'list_1', 'is', 'an', 'example']

扩展()函数

extend() 函数用于将可列表(列表、字符串或元组)的每个元素追加到列表的末尾。这将增加列表的长度,增加作为参数传递的 iterable 元素的数量。

语法


list_name1.extend(iterable)

示例:


list_1 = ['The', 'list_1']

# Using the method
list_1.extend(['is', 'an', 'example'])
list_1.extend('javatpoint')

# Displaying the list
print ("The extended elements in the given list are: ", list_1)

输出:

The extended elements in the given list are:  ['The', 'list_1', 'is', 'an', 'example', 'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't']

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

| 追加()函数 | 插入()函数 | 扩展()函数 | | 参数中传递的元素被添加到列表的末尾。 | 参数中传递的元素被添加到列表中提到的索引处。 | iterable 的每个元素都作为 ab 参数在列表末尾添加时传递。 | | 作为参数传递的元素将作为单个元素添加,不做任何更改。 | 参数中传递的元素将作为单个元素添加到所需位置,不做任何更改。 | 作为参数传递的 iterable 将在列表的末尾追加它的每个元素。 | | 列表的长度将增加 1。 | 列表的长度将增加 1。 | 列表的长度将随着列表中元素的数量而增加。 | | append()函数的时间复杂度为 O(1)。 | insert()函数的线性复杂度为 O(n)。 | extend()函数的时间复杂度为 O(k),其中“k”是可迭代的长度。 |

让我们在一个程序中比较这三种方法:


list_name1 = ['this', 'is', 'LIST_1']
list_name2 = ['this', 'is', 'of', 'LIST_2']
list_name3 = ['this', 'is', 'LIST_3']

S = ['Example_1', 'Example_2']

# Using methods
list_name1.append(S)
list_name2.insert(2, S)
list_name3.extend(S)

# Displaying lists
print ("The appended elements in the given list are: ", list_name1)
print ("The inserted elements in the given list are: ", list_name2)
print ("The extended elements in the given list are: ", list_name3)

输出:

The appended elements in the given list are: ['this', 'is', 'LIST_1', ['Example_1', 'Example_2']]
The inserted elements in the given list are: ['this', 'is', ['Example_1', 'Example_2'], 'of', 'LIST_2']
The extended elements in the given list are: ['this', 'is', 'LIST_3', 'Example_1', 'Example_2']

结论

在本教程中,我们讨论了在 Python 中修改列表的不同方法。我们还解释了 Python 列表中 append()、insert()和 extend()函数之间的区别。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。