Python 中append、extend和insert的区别

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()函数之间的区别。


原创文章,作者:ALRIW,如若转载,请注明出处:https://www.506064.com/n/128769.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
ALRIWALRIW
上一篇 2024-10-03 23:25
下一篇 2024-10-03 23:25

相关推荐

  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29

发表回复

登录后才能评论