Skip to content

列表

一句话解释

列表是 有序, 可变集合的 项 in Python, 编写 as [1, 2, 3].

用大白话说

Python 列表是 有序集合那个能 持有任何 mix 的类型 — 数字, 字符串, 偶数 other 列表. 列表是 可变, meaning 你能 添加, 移除, or 更改元素之后创建它们. 它们是 编写使用 square brackets: [1, 2, 3].

Lists are the go-to data structure in Python for sequences of items. You can iterate over them with a for loop, access elements by index (my_list[0]), slice them (my_list[1:3]), and use methods like append(), 移除(), and 排序().

Python lists are similar to JavaScript arrays, but they are more flexible with types — a single list can hold integers, strings, and objects together. They are implemented as dynamic arrays under the hood, so appending is fast but inserting at the front is slow.

示例

python
fruits = ["apple", "banana", "cherry"]

print(fruits[0])       # "apple"
print(len(fruits))    # 3

fruits.append("date")
print(fruits)         # ["apple", "banana", "cherry", "date"]

for fruit in fruits:
    print(fruit)

列表 support indexing, len(), append(), and iteration 使用 for 循环.

常见混淆

  • 容易混淆的对象: 数组

    关键区别: In Python, 构建-in 序列类型是 called 列表 and 能持有 mixed 类型. Other languages (JavaScript, C) call 相同概念数组. Python's 数组模块 exists 但是 for homogeneous numeric 数据 and 是很少 used.

  • 容易混淆的对象: tuple

    关键区别: 列表是 可变 — 你能 更改, 添加, and 移除元素. tuple 是不可变 — 一次创建, 它 cannot 是更改. Use 列表 for 更改序列, tuples for fixed 记录.

相关术语

相关资源

相关课程

相关项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表