GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — Python 列表(List) | 菜鸟教程

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

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan
Python 基础教程
Python 基础教程 Python 简介 Python 环境搭建 Python 中文编码 Python VS Code Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 Python 循环语句 Python While 循环语句 Python for 循环语句 Python 循环嵌套 Python break 语句 Python continue 语句 Python pass 语句 Python Number(数字) Python 字符串 Python 列表(List) Python 元组 Python 字典(Dictionary) Python 日期和时间 Python 函数 Python 模块 Python 文件I/O Python File 方法 Python 异常处理 Python OS 文件/目录方法 Python 内置函数

Python 高级教程

Python 面向对象 Python 正则表达式 Python CGI 编程 Python MySQL Python 网络编程 Python SMTP Python 多线程 Python XML 解析 Python GUI 编程(Tkinter) Python2.x 与 3​​.x 版本区别 Python IDE Python JSON Python AI 绘画 Python 100例 Python 测验
Python 字符串
Python 元组

Python 列表(List)

序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。

Python有6个序列的内置类型,但最常见的是列表和元组。

序列都可以进行的操作包括索引,切片,加,乘,检查成员。

此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法。

列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。

列表的数据项不需要具有相同的类型

创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。如下所示:

list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ["a", "b", "c", "d"]

与字符串的索引一样,列表索引从0开始。列表可以进行截取、组合等。


访问列表中的值

使用下标索引来访问列表中的值,同样你也可以使用方括号的形式截取字符,如下所示:

实例(Python 2.0+)

#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]

以上实例输出结果:

list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]

更新列表

你可以对列表的数据项进行修改或更新,你也可以使用append()方法来添加列表项,如下所示:

实例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- list = [] ## 空列表 list.append('Google') ## 使用 append() 添加元素 list.append('Runoob') print list

注意:我们会在接下来的章节讨论append()方法的使用

以上实例输出结果:

['Google', 'Runoob']

删除列表元素

可以使用 del 语句来删除列表的元素,如下实例:

实例(Python 2.0+)

#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000] print list1 del list1[2] print "After deleting value at index 2 : " print list1

以上实例输出结果:

['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]

注意:我们会在接下来的章节讨论remove()方法的使用


Python列表脚本操作符

列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。

如下所示:

Python 表达式结果 描述
len([1, 2, 3])3长度
[1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6]组合
['Hi!'] * 4['Hi!', 'Hi!', 'Hi!', 'Hi!']重复
3 in [1, 2, 3]True元素是否存在于列表中
for x in [1, 2, 3]: print x,1 2 3迭代

Python列表截取

Python 的列表截取实例如下:

>>>L = ['Google', 'Runoob', 'Taobao'] >>> L[2] 'Taobao' >>> L[-2] 'Runoob' >>> L[1:] ['Runoob', 'Taobao'] >>>

描述:

Python 表达式结果 描述
L[2]'Taobao'读取列表中第三个元素
L[-2]'Runoob'读取列表中倒数第二个元素
L[1:]['Runoob', 'Taobao']从第二个元素开始截取列表

Python列表函数&方法

Python包含以下函数:

序号函数
1cmp(list1, list2)
比较两个列表的元素
2len(list)
列表元素个数
3max(list)
返回列表元素最大值
4min(list)
返回列表元素最小值
5list(seq)
将元组转换为列表

Python包含以下方法:

序号方法
1list.append(obj)
在列表末尾添加新的对象
2list.count(obj)
统计某个元素在列表中出现的次数
3list.extend(seq)
在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
4list.index(obj)
从列表中找出某个值第一个匹配项的索引位置
5list.insert(index, obj)
将对象插入列表
6list.pop([index=-1])
移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
7list.remove(obj)
移除列表中某个值的第一个匹配项
8list.reverse()
反向列表中元素
9list.sort(cmp=None, key=None, reverse=False)
对原列表进行排序
AI 思考中...
Python 字符串
Python 元组

12 篇笔记 写笔记

  1. #0

       tianqixin

      429***[email protected]

      参考地址

    244

    python 创建二维列表,将需要的参数写入 cols 和 rows 即可

    list_2d = [[0 for col in range(cols)] for row in range(rows)]
    

    实例:

    >>> list_2d = [ [0 for i in range(5)] for i in range(5)]
    >>> list_2d[0].append(3)
    >>> list_2d[0].append(5)
    >>> list_2d[2].append(7)
    >>> list_2d
    [[0, 0, 0, 0, 0, 3, 5], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 7], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
    
    tianqixin

       tianqixin

      429***[email protected]

      参考地址

    10年前 (2017-02-25)
  2. #0

       python list

      791***[email protected]

    138
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    list01 = ['runoob', 786, 2.23, 'john', 70.2]
    list02 = [123, 'john']
    
    print list01
    print list02
    
    # 列表截取
    
    print list01[0]
    print list01[-1]
    print list01[0:3]
    
    # 列表重复
    
    print list01 * 2
    
    # 列表组合
    
    print list01 + list02
    
    # 获取列表长度
    
    print len(list01)
    
    # 删除列表元素
    
    del list02[0]
    print list02
    
    # 元素是否存在于列表中
    
    print 'john' in list02  # True
    
    # 迭代
    
    for i in list01:
        print i
    
    # 比较两个列表的元素
    
    print cmp(list01, list02)
    
    # 列表最大/最小值
    
    print max([0, 1, 2, 3, 4])
    print min([0, 1])
    
    # 将元组转换为列表
    
    aTuple = (1,2,3,4)
    list03 = list(aTuple)
    print list03
    
    # 在列表末尾添加新的元素
    
    list03.append(5)
    print list03
    
    # 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
    
    list03.extend(list01)
    print list03
    
    # 统计某个元素在列表中出现的次数
    
    print list03.count(1)
    
    # 从列表中找出某个值第一个匹配项的索引位置
    
    print list03.index('john')
    
    # 将对象插入列表
    
    list03.insert(0, 'hello')
    print list03
    
    # 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
    
    print list03.pop(0)
    print list03
    
    # 移除列表中某个值的第一个匹配项
    
    list03.remove(1)
    print list03
    
    # 反向列表中元素
    
    list03.reverse()
    print list03
    
    # 对原列表进行排序
    
    list03.sort()
    print list03
    
    python list

       python list

      791***[email protected]

    10年前 (2017-03-29)
  3. #0

       若能绽放光芒

      740***[email protected]

    40
    >>> list4=[123,["das","aaa"],234]
    >>> list4
    >>> "aaa" in list4                  #in只能判断一个层次的元素
    False
    >>> "aaa" in list4[1]           #选中列表中的列表进行判断
    True
    >>> list4[1][1]
    'aaa'
    若能绽放光芒

       若能绽放光芒

      740***[email protected]

    9年前 (2017-12-15)
  4. #0

       天堂的风

      155***[email protected]

    33

    遍历嵌套的列表:

    num_list = [[1,2,3],[4,5,6]]
    for i in num_list:
        for j in i:
            print(j)

    输出结果:

    1
    2
    3
    4
    5
    6
    天堂的风

       天堂的风

      155***[email protected]

    9年前 (2018-03-13)
  5. #0

       旭日再现

      xsx***[email protected]

    17

    [:-1] 表示从第一个元素遍历到倒数第二个元素:

    # -*- coding: UTF-8 -*-
    
    list1 = [1,2,3,4,5,]
    
    print list1
    
    # 列表截取
    print list1[:-1]

    输出结果:

    [1, 2, 3, 4, 5]
    [1, 2, 3, 4]
    旭日再现

       旭日再现

      xsx***[email protected]

    9年前 (2018-03-13)
  6. #0

       七文

      zha***[email protected]

    46

    列表里 a 与 a[:] 不同。

    我们可以通过函数 id() 来查看:

    a = [1, 2, 3]
    id(a)
    id(a[:])

    会发现得到的两个值不同。

    或者直接运行:

    a is a[:]

    返回值将是:False。

    简单来说,a[:] 是创建 a 的一个副本,这样我们在代码中对 a[:] 进行操作后,就不会改变 a 的值了。而若直接对 a 进行操作,那么 a 的值会收到一些操作的影响,如 append() 等。

    七文

       七文

      zha***[email protected]

    9年前 (2018-05-24)
  7. #0

       岁月流金

      145***[email protected]

    23

    针对列表无法正常输出汉字的解决方法:

    #encoding=utf-8
    
    import json
    
    list_words = [ '你', '我', '他' ]
    print( list_words )                                        # 无法正常显示汉字
    print( str(list_words).decode( 'string_escape' ) )         # 正常显示汉字
    
    list_words_result = json.dumps( list_words, encoding='UTF-8', ensure_ascii=False )
    print( list_words_result )

    输出结果为:

    ['\xe4\xbd\xa0', '\xe6\x88\x91', '\xe4\xbb\x96']
    ['你', '我', '他']
    ["你", "我", "他"]
    岁月流金

       岁月流金

      145***[email protected]

    8年前 (2018-07-24)
  8. #0

       心猿意马不可取

      144***[email protected]

    43

    remove 和 del 之间的区别:

    >>> a=[1,2,3,5,4,2,6]
    >>> a.remove(a[5])
    >>> a
    [1, 3, 5, 4, 2, 6]

    说明 remove 移除的是列表中元素的位置。

    >>> a=[1,2,3,5,4,2,6]
    >>> del(a[5])
    >>> a
    [1, 2, 3, 5, 4, 6]

    说明 del 删除是按索引来的,索引起始位置为 0。

    心猿意马不可取

       心猿意马不可取

      144***[email protected]

    8年前 (2018-08-08)
  9. #0

       Lanyue

      col***[email protected]

      参考地址

    18

    清空列表中的多项空值:

    test = ['a','','b','','c','','']
    test = [i for i in test if i != '']
    
    print(test)

    输出结果为:

    ['a', 'b', 'c']
    Lanyue

       Lanyue

      col***[email protected]

      参考地址

    8年前 (2018-10-18)
  10. #0

       Paris

      160***[email protected]

    30

    Python 列表切片应用

    # -*- coding: UTF-8 -*-
    
    s = 'abcdefg'
    # 返回从起始位置到索引位置 2 处的字符串切片
    print(s[:3]) # 输出 'abc'
    
    # 返回从第三个索引位置到结尾的字符串切片
    print(s[3:]) # 输出 'defg'
    
    # 字符串逆序输出
    print(s[::-1]) # 输出 'gfedcba'
    
    # 输出从开始位置间隔一个字符组成的字符串
    print(s[::2]) # 输出 'aceg'
    print(range(10)[::2])  # 输出偶数:[0, 2, 4, 6, 8]
    
    # 它们也可以相互结合使用。
    # 从索引位置 6 到索引位置 2,逆向间隔一个字符
    print(s[6:2:-2]) # 输出'ge'
    Paris

       Paris

      160***[email protected]

    8年前 (2018-10-24)
  11. #0

       小jia进化

      jia***[email protected]

    13

    Python 数组的步长操作:

    a = [0,2,3,4,5,6,7,8,22,44,55]
    
    # 2 表示起始坐标,10 表示终止坐标,3 表示步长,即每移动 3 个位置取值
    b = a[2:10:3]
    print(b)

    输出结果为:

    [3, 6, 22]
    小jia进化

       小jia进化

      jia***[email protected]

    8年前 (2019-04-19)
  12. #0

       水月洞天

      593***[email protected]

    40

    列表解析 List Comprehensions

    表达式:[expression for iter_val in iterable if cond_expr]
    • [expression]:最后执行的结果
    • [for iter_val in iterable]:这个可以是一个多层循环
    • [if cond_expr]:两个for间是不能有判断语句的,判断语句只能在最后;顺序不定,默认是左到右。

    讨论下多个 for 循环的执行顺序。

    从左到右:

    >>> print[(x,y)for x in [1,2]for y in [3,4]]
    [(1, 3), (1, 4), (2, 3), (2, 4)]
    >>> print[(x,y)for x in [1,2]for y in [3,x]]
    [(1, 3), (1, 1), (2, 3), (2, 2)]
    >>> 

    从右到左:

    >>> print[(x,y)for x in [1,y]for y in [3,4]]
    [(1, 3), (1, 4), (2, 3), (2, 4)]
    >>> 
    
    水月洞天

       水月洞天

      593***[email protected]

    8年前 (2019-05-06)

点我分享笔记

取消

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
分类导航
  • Python / 数据科学
    • Python 教程
    • Python2.x 教程
    • FastAPI 教程
    • Flask 教程
    • Django 教程
    • NumPy 教程
    • Pandas 教程
    • SciPy 教程
    • Matplotlib 教程
    • Dash 教程
    • Jupyter Notebook 教程
    • Pillow 教程
    • 量化交易
    • R 教程
    • Julia 教程
  • AI / 智能开发
    • AI Agent(智能体)
    • AI(人工智能)
    • AI 应用案例
    • Codex 教程
    • Vibe Coding 教程
    • Claude Code
    • OpenCode
    • Skills(技能)
    • Ollama 教程
    • Hermes Agent
    • DeepSeek Harness
    • Pi Agent
    • AI 数学基础
    • TensorFlow 教程
    • PyTorch 教程
    • Scikit-learn 教程
    • 机器 教程
    • LangChain
    • 自然语言处理 NLP
    • OpenCV 教程
    • Selenium 教程
    • Playwright 教程
  • 前端开发
    • HTML 教程
    • HTML5 教程
    • CSS 教程
    • CSS3 教程
    • JavaScript 教程
    • HTML DOM 教程
    • TypeScript 教程
    • AJAX 教程
    • JSON 教程
    • Tailwind CSS 教程
    • Bootstrap4 教程
    • Bootstrap5 教程
    • Foundation 教程
    • Vue.js 教程
    • Vue3 教程
    • React 教程
    • Next.js 教程
    • Three.js 教程
    • AngularJS 教程
    • Angular 教程
    • jQuery 教程
    • jQuery UI 教程
    • jQuery EasyUI 教程
    • ECharts 教程
    • Chart.js 教程
    • Highcharts 教程
    • Google 地图 教程
    • SVG 教程
    • Font Awesome 教程
  • 后端开发
    • Node.js 教程
    • Electron 教程
    • PHP 教程
    • Java 教程
    • Go 教程
    • Rust 教程
    • C# 教程
    • Unity 教程
    • Servlet 教程
    • JSP 教程
    • ASP 教程
    • AppML 教程
    • VBScript 教程
    • Swagger 教程
    • RESTful API 教程
    • Docker 教程
    • Linux 教程
    • ZooKeeper 教程
  • 数据库
    • SQL 教程
    • MySQL 教程
    • PostgreSQL 教程
    • SQLite 教程
    • MongoDB 教程
    • Redis 教程
    • Memcached 教程
  • 移动开发
    • Android 教程
    • Flutter 教程
    • Ionic 教程
    • jQuery Mobile 教程
    • Swift 教程
    • Kotlin 教程
  • DevOps / 工程化
    • Git 教程
    • SVN 教程
    • CMake 教程
    • Maven 教程
    • VS Code 教程
    • Obsidian 教程
    • PyCharm 教程
    • Eclipse 教程
    • Markdown 教程
    • LaTeX 教程
  • 编程语言
    • C 教程
    • C++ 教程
    • Zig 教程
    • Scala 教程
    • Ruby 教程
    • Perl 教程
    • Lua 教程
    • Dart 教程
    • 汇编语言 教程
    • Verilog 教程
  • 计算机基础
    • 计算机组成原理
    • 数据结构与算法
    • C 语言数据结构与算法
    • 设计模式
    • Python 设计模式
    • 正则表达式
    • HTTP 教程
    • TCP/IP 教程
    • 网络协议
    • W3C 教程
  • XML / Web Service
    • XML 教程
    • DTD 教程
    • XML DOM 教程
    • XSLT 教程
    • XPath 教程
    • XQuery 教程
    • XLink 教程
    • XPointer 教程
    • XML Schema 教程
    • XSL-FO 教程
    • Web Service 教程
    • WSDL 教程
    • SOAP 教程
    • RSS 教程
    • RDF 教程
  • .NET
    • ASP.NET 教程
    • MVC 教程
    • Razor 教程
    • Web Forms 教程
    • Web Pages 教程
    • PowerShell 教程
  • 网站建设
    • 网站建设指南
    • 浏览器信息
    • 网站主机教程
    • 网站品质

字节 Coding Plan 限时 9.9 元起,支持 Deepseek-V4、GLM-5.3、Doubao-Seed-Evolving、Kimi-K3。
百度 Dumate 百度推出的桌面级 AI 智能体。 输入激活码送 4000 积分
使用文档
Trae AI 原生开发环境,让编程更高效。新用户注册赠2500积分,管理项目,也交付成果。
Advertisement
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.