菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 3 教程
Python3 教程 Python3 简介 Python3 环境搭建 Python3 VScode Python3 基础语法 Python3 基本数据类型 Python3 数据类型转换 Python3 解释器 Python3 注释 Python3 运算符 Python3 数字(Number) Python3 字符串 Python3 列表 Python3 元组 Python3 字典 Python3 集合 Python3 条件控制 Python3 循环语句 Python3 编程第一步 Python3 推导式 Python3 迭代器与生成器 Python3 with Python3 函数 Python3 lambda Python3 装饰器 Python3 数据结构 Python3 模块 Python __name__ Python3 输入和输出 Python3 File Python3 OS Python3 错误和异常 Python3 面向对象 Python3 命名空间/作用域 Python 虚拟环境的创建 Python 类型注解 Python3 标准库概览 Python3 实例 Python 测验

Python3 高级教程

Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQt Python for 循环 Python while 循环
(追記) (追記ここまで)

Python3 List append()方法

Python3 列表 Python3 列表


描述

append() 方法用于在列表末尾添加新的对象。

语法

append()方法语法:

list.append(obj)

参数

  • obj -- 添加到列表末尾的对象。

返回值

该方法无返回值,但是会修改原来的列表。

实例

以下实例展示了 append()函数的使用方法:

实例

#!/usr/bin/python3

list1 = ['Google', 'Runoob', 'Taobao']
list1.append('Baidu')
print ("更新后的列表 : ", list1)

以上实例输出结果如下:

更新后的列表 : ['Google', 'Runoob', 'Taobao', 'Baidu']

队列是一种先进先出(FIFO)的数据结构,我们可以使用列表来实现队列的基本功能。

  • append() 方法向队列的末尾添加一个元素。
  • pop() 方法从队列的开头删除一个元素并返回它。

实例

queue = []

# 添加元素到队列的末尾
queue.append('A')
queue.append('B')
queue.append('C')

# 从队列的开头删除元素并返回
print(queue.pop(0)) # A
print(queue.pop(0)) # B
print(queue.pop(0)) # C

以上实例中,我们创建了一个空的列表作为队列,然后使用 append() 方法向队列的末尾添加了三个元素。接下来,我们使用 pop() 方法从队列的开头删除元素并返回它们。由于队列是一个先进先出的数据结构,所以我们得到的输出结果是 'A'、'B' 和 'C'。

Python3 列表 Python3 列表

AI 思考中...

2 篇笔记 写笔记

  1. #0

    Suck My Gun

    307***[email protected]

    129

    定义了两个函数一个用了extend()方法,一个用了append()方法

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    def changeextend(str):
     "print string with extend"
     mylist.extend([40,50,60]);
     print ("print string mylist:",mylist)
     return
    def changeappend(str):
     "print string with append" 
     mylist.append( [7,8,9] )
     print("print string mylist:",mylist )
     return
    mylist = [10,20,30]
    changeextend( mylist );
    print ("print extend mylist:", mylist )
    changeappend( mylist );
    print ("print append mylist:", mylist )
    

    输出结果:

    • print string mylist: [10, 20, 30, 40, 50, 60]
    • print extend mylist: [10, 20, 30, 40, 50, 60]
    • print string mylist: [10, 20, 30, 40, 50, 60, [7, 8, 9]]
    • print append mylist: [10, 20, 30, 40, 50, 60, [7, 8, 9]]

    通过比较可知:

    1. 列表可包含任何数据类型的元素,单个列表中的元素无须全为同一类型。
    2. append() 方法向列表的尾部添加一个新的元素。
    3. 列表是以类的形式实现的。"创建"列表实际上是将一个类实例化。因此,列表有多种方法可以操作。extend()方法只接受一个列表作为参数,并将该参数的每个元素都添加到原有的列表中。

    Suck My Gun

    307***[email protected]

    9年前 (2017年05月09日)
  2. #0

    append() 是浅拷贝,如果在 append 一个对象时,需要特别注意:

    >>>alist = []
    >>> num = [2]
    >>> alist.append( num )
    >>> id( num ) == id( alist[0] )
    True

    如果使用 num[0]=3,改变 num 后,alist[0] 也随之改变。

    如不希望,需要使用 alist.append( copy.deepcopy( num ) )

    更多参考文章

    8年前 (2018年07月17日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /