《LearnPython-Python学习笔记》-在Python中创建空列表

admin 2025-11-07 00:58:36 编程 来源:ZONE.CI 全球网 0 阅读模式
  • 🔹使用方括号 (🔹 Using Square Brackets)
  • 🔸使用list()构造函数 (🔸 Using the list() Constructor)
    • 测试[] : (Testing []:)
    • 测试list() : (Testing list():)
    • 将元素添加到空列表 (Add Elements to an Empty List)

    🔹使用方括号 (🔹 Using Square Brackets)

    You can create an empty list with an empty pair of square brackets, like this: 您可以使用一对空的方括号创建一个空列表,如下所示:💡 Tip: We assign the empty list to a variable to use it later in our program.提示:我们将空列表分配给变量,以便稍后在程序中使用它。For example:例如:

    1. num = []

    The empty list will have length 0, as you can see right here:空列表的长度为0 ,您可以在此处看到:

    1. num = []
    2. len(num)
    3. 0

    🔸使用list()构造函数 (🔸 Using the list() Constructor)

    Alternatively, you can create an empty list with the type constructor list(), which creates a new list object.另外,您可以使用构造函数list()创建一个空列表,这将创建一个新的列表对象。根据Python文档 :如果未提供任何参数,则构造函数将创建一个新的空列表[] 。💡 Tip: This creates a new list object in memory and since we didn’t pass any arguments to list(), an empty list will be created.提示:这将在内存中创建一个新的列表对象,由于我们没有将任何参数传递给list() ,因此将创建一个空列表。For example:例如:

    1. num = list()

    This empty list will have length 0, as you can see right here:空列表的长度为0 ,您可以在此处看到:

    1. num = list()
    2. len(num)
    3. 0

    测试[] : (Testing []:)

    timeit.timeit(‘[]’, number=10**4)0.0008467000000109692

    测试list() : (Testing list():)

    timeit.timeit(‘list()’, number=10**4)0.002867799999989984

    可以看到[]比list()快得多。 此测试相差约0.002秒:>>> 0.002867799999989984 - 0.00084670000001096920.0020210999999790147

    将元素添加到空列表 (Add Elements to an Empty List)

    • append() adds the element to the end of the list.
    • append()将元素添加到列表的末尾。
    • insert() adds the element at the particular index of the list that you choose.
    • insert()将元素添加到您选择的列表的特定索引处。

    https://blog.csdn.net/cumian9828/article/details/108098026

    以太坊cppgolang区别 编程

    以太坊cppgolang区别

    以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
    progolang 编程

    progolang

    Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
    golangn个发送者 编程

    golangn个发送者

    Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
    golang技能图谱 编程

    golang技能图谱

    从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
    评论:0   参与:  9