9.md 6.2 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
# 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

```

W
wizardforcel 已提交
41
**注意**
W
init  
wizardforcel 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

列表是可变的。

## 访问列表中的元素

* * *

您可以使用索引运算符(`[]`)访问列表中的各个元素。 列表索引从`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

```

W
wizardforcel 已提交
60
## 常用列表操作
W
init  
wizardforcel 已提交
61 62 63 64 65

* * *

| 方法名称 | 描述 |
| --- | --- |
W
wizardforcel 已提交
66 67 68 69 70 71 72 73 74 75
| `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`循环中从左到右遍历元素。 |
W
init  
wizardforcel 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

## 使用函数列出示例

* * *

```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`索引也是可选的,如果省略,它将被设置为列表的最后一个索引。

**note:**

W
wizardforcel 已提交
129
如果为`start >= end`,则`list[start : end]`将返回一个空列表。 如果`end`指定的位置超出列表的`end`,则 Python 将使用`end`的列表长度。
W
init  
wizardforcel 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

## 列表中的`+`和`*`运算符

* * *

`+`运算符加入两个列表。

```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

```

W
wizardforcel 已提交
177
## 使用`for`循环遍历列表
W
init  
wizardforcel 已提交
178 179 180

* * *

W
wizardforcel 已提交
181
如前所述,列表是一个序列并且也是可迭代的。 意味着您可以使用`for`循环遍历列表的所有元素。
W
init  
wizardforcel 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

```py
>>> list = [1,2,3,4,5]
>>> for i in list:
... print(i, end=" ")
1 2 3 4 5

```

## 带有返回类型的常用列表方法

* * *

| Method name | Description |
| --- | --- |
W
wizardforcel 已提交
197 198 199 200 201 202 203 204 205
| `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()`删除并返回列表中的最后一个元素。 |
W
init  
wizardforcel 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

```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]
>>>

```

W
wizardforcel 已提交
246
## 列表推导式
W
init  
wizardforcel 已提交
247 248 249 250 251 252 253

* * *

**note:**

本主题需要具有 [Python 循环](/loops/)的使用知识。

W
wizardforcel 已提交
254
列表理解为创建列表提供了一种简洁的方法。 它由包含表达式的方括号组成,后跟`for`子句,然后是零个或多个`for``if`子句。
W
init  
wizardforcel 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

这里有些例子:

```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 字典。

* * *

* * *