# Python 列表
> 原文: [https://thepythonguru.com/python-lists/](https://thepythonguru.com/python-lists/)
* * *
于 2020 年 1 月 7 日更新
* * *
列表類型是 python 的列表類定義的另一種序列類型。 列表允許您以非常簡單的方式添加,刪除或處理元素。 列表與數組非常相似。
## 在 python 中創建列表
* * *
您可以使用以下語法創建列表。
```py
>>> l = [1, 2, 3, 4]
```
在此,列表中的每個元素都用逗號分隔,并用一對方括號(`[]`)包圍。 列表中的元素可以是相同類型或不同類型。 例如:
```py
l2 = ["this is a string", 12]
```
創建列表的其他方式。
```py
list1 = list() # Create an empty list
list2 = list([22, 31, 61]) # Create a list with elements 22, 31, 61
list3 = list(["tom", "jerry", "spyke"]) # Create a list with strings
list5 = list("python") # Create a list with characters p, y, t, h, o, n
```
**注意**:
列表是可變的。
## 訪問列表中的元素
* * *
您可以使用索引運算符(`[]`)訪問列表中的各個元素。 列表索引從`0`開始。
```py
>>> l = [1,2,3,4,5]
>>> l[1] # access second element in the list
2
>>> l[0] # access first element in the list
1
```
## 常用列表操作
* * *
| 方法名稱 | 描述 |
| --- | --- |
| `x in s` | 如果元素`x`在序列`s`中,則為`True`,否則為`False` |
| `x not in s` | 如果元素`x`不在序列`s`中,則為`True`,否則為`False` |
| `s1 + s2` | 連接兩個序列`s1`和`s2` |
| `s * n`,`n * s` | 連接序列`s`的`n`個副本 |
| `s[i]` | 序列`s`的第`i`個元素。 |
| `len(s)` | 序列`s`的長度,也就是元素數量。 |
| `min(s)` | 序列`s`中最小的元素。 |
| `max(s)` | 序列`s`中最大的元素。 |
| `sum(s)` | 序列`s`中所有數字的總和。 |
| `for`循環 | 在`for`循環中從左到右遍歷元素。 |
## 列表函數示例
* * *
```py
>>> list1 = [2, 3, 4, 1, 32]
>>> 2 in list1
True
>>> 33 not in list1
True
>>> len(list1) # find the number of elements in the list
5
>>> max(list1) # find the largest element in the list
32
>>> min(list1) # find the smallest element in the list
1
>>> sum(list1) # sum of elements in the list
42
```
## 列表切片
* * *
切片運算符(`[start:end]`)允許從列表中獲取子列表。 它的工作原理類似于字符串。
```py
>>> list = [11,33,44,66,788,1]
>>> list[0:5] # this will return list starting from index 0 to index 4
[11,33,44,66,788]
```
```py
>>> list[:3]
[11,33,44]
```
類似于字符串`start`的索引是可選的,如果省略,它將為`0`。
```py
>>> list[2:]
[44,66,788,1]
```
`end`索引也是可選的,如果省略,它將被設置為列表的最后一個索引。
**注意**:
如果為`start >= end`,則`list[start : end]`將返回一個空列表。 如果`end`指定的位置超出列表的`end`,則 Python 將使用`end`的列表長度。
## 列表中的`+`和`*`運算符
* * *
`+`運算符加入兩個列表。
```py
>>> list1 = [11, 33]
>>> list2 = [1, 9]
>>> list3 = list1 + list2
>>> list3
[11, 33, 1, 9]
```
`*`操作符復制列表中的元素。
```py
>>> list4 = [1, 2, 3, 4]
>>> list5 = list4 * 3
>>> list5
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
```
## `in`或`not in`運算符
* * *
`in`運算符用于確定列表中是否存在元素。 成功則返回`True`;失敗則返回`False`。
```py
>>> list1 = [11, 22, 44, 16, 77, 98]
>>> 22 in list1
True
```
同樣,`not in`與`in`運算符相反。
```py
>>> 22 not in list1
False
```
## 使用`for`循環遍歷列表
* * *
如前所述,列表是一個序列并且也是可迭代的。 意味著您可以使用`for`循環遍歷列表的所有元素。
```py
>>> list = [1,2,3,4,5]
>>> for i in list:
... print(i, end=" ")
1 2 3 4 5
```
## 常用列表方法和返回類型
* * *
| 方法 | 描述 |
| --- | --- |
| `append(x: object): None` | 在列表的末尾添加元素`x`并返回`None`。 |
| `count(x: object): int` | 返回元素`x`在列表中出現的次數。 |
| `append(l: list): None` | 將`l`中的所有元素附加到列表中并返回`None`。 |
| `index(x: object): int` | 返回列表中第一次出現的元素`x`的索引 |
| `insert(index: int, x: object): None` | 在給定索引處插入元素`x`。 請注意,列表中的第一個元素具有索引`0`并返回`None`。 |
| `remove(x: object): None` | 從列表中刪除第一次出現的元素`x`并返回`None` |
| `reverse(): None` | 反轉列表并返回`None` |
| `sort(): None` | 按升序對列表中的元素進行排序并返回`None`。 |
| `pop(i): object` | 刪除給定位置的元素并返回它。 參數`i`是可選的。 如果未指定,則`pop()`刪除并返回列表中的最后一個元素。 |
```py
>>> list1 = [2, 3, 4, 1, 32, 4]
>>> list1.append(19)
>>> list1
[2, 3, 4, 1, 32, 4, 19]
>>> list1.count(4) # Return the count for number 4
2
>>> list2 = [99, 54]
>>> list1.extend(list2)
>>> list1
[2, 3, 4, 1, 32, 4, 19, 99, 54]
>>> list1.index(4) # Return the index of number 4
2
>>> list1.insert(1, 25) # Insert 25 at position index 1
>>> list1
[2, 25, 3, 4, 1, 32, 4, 19, 99, 54]
>>>
>>> list1 = [2, 25, 3, 4, 1, 32, 4, 19, 99, 54]
>>> list1.pop(2)
3
>>> list1
[2, 25, 4, 1, 32, 4, 19, 99, 54]
>>> list1.pop()
54
>>> list1
[2, 25, 4, 1, 32, 4, 19, 99]
>>> list1.remove(32) # Remove number 32
>>> list1
[2, 25, 4, 1, 4, 19, 99]
>>> list1.reverse() # Reverse the list
>>> list1
[99, 19, 4, 1, 4, 25, 2]
>>> list1.sort() # Sort the list
>>> list1
[1, 2, 4, 4, 19, 25, 99]
>>>
```
## 列表推導式
* * *
**注意**:
本主題需要具有 [Python 循環](/loops/)的使用知識。
列表理解為創建列表提供了一種簡潔的方法。 它由包含表達式的方括號組成,后跟`for`子句,然后是零個或多個`for`或`if`子句。
這里有些例子:
```py
>>> list1 = [ x for x in range(10) ]
>>> list1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>>
>>> list2 =?[ x + 1 for x in range(10) ]
>>> list2
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>
>>>
>>>?list3 = [ x for x in range(10) if x % 2 == 0 ]
>>> list3
[0, 2, 4, 6, 8]
>>>
>>>
>>>?list4 = [ x *2 for x in range(10) if x % 2 == 0 ]
[0, 4, 8, 12, 16]
```
在下一個教程中,我們將學習 python 字典。
* * *
* * *
- 初級 Python
- python 入門
- 安裝 Python3
- 運行 python 程序
- 數據類型和變量
- Python 數字
- Python 字符串
- Python 列表
- Python 字典
- Python 元組
- 數據類型轉換
- Python 控制語句
- Python 函數
- Python 循環
- Python 數學函數
- Python 生成隨機數
- Python 文件處理
- Python 對象和類
- Python 運算符重載
- Python 繼承與多態
- Python 異常處理
- Python 模塊
- 高級 Python
- Python *args和**kwargs
- Python 生成器
- Python 正則表達式
- 使用 PIP 在 python 中安裝包
- Python virtualenv指南
- Python 遞歸函數
- __name__ == "__main__"是什么?
- Python Lambda 函數
- Python 字符串格式化
- Python 內置函數和方法
- Python abs()函數
- Python bin()函數
- Python id()函數
- Python map()函數
- Python zip()函數
- Python filter()函數
- Python reduce()函數
- Python sorted()函數
- Python enumerate()函數
- Python reversed()函數
- Python range()函數
- Python sum()函數
- Python max()函數
- Python min()函數
- Python eval()函數
- Python len()函數
- Python ord()函數
- Python chr()函數
- Python any()函數
- Python all()函數
- Python globals()函數
- Python locals()函數
- 數據庫訪問
- 安裝 Python MySQLdb
- 連接到數據庫
- MySQLdb 獲取結果
- 插入行
- 處理錯誤
- 使用fetchone()和fetchmany()獲取記錄
- 常見做法
- Python:如何讀取和寫入文件
- Python:如何讀取和寫入 CSV 文件
- 用 Python 讀寫 JSON
- 用 Python 轉儲對象