167.md 5.7 KB
Newer Older
W
wizardforcel 已提交
1
# Python 列表
W
init  
wizardforcel 已提交
2 3 4

> 原文: [https://javabeginnerstutorial.com/python-tutorial/python-list-2/](https://javabeginnerstutorial.com/python-tutorial/python-list-2/)

W
wizardforcel 已提交
5
我们已经知道 python 列表可以用方括号之间的逗号分隔值编写,并且元素不必来自同一类型。 如果您倾向于忘记此功能,那么后者自然会引起一些混乱和错误。
W
init  
wizardforcel 已提交
6

W
wizardforcel 已提交
7
```py
W
init  
wizardforcel 已提交
8 9 10 11
>>> [1, 'Python', 3.14]
[1, 'Python', 3.14]
```

W
wizardforcel 已提交
12
### 访问 python 列表中的元素
W
init  
wizardforcel 已提交
13

W
wizardforcel 已提交
14
访问列表中的值需要使用它们的索引。 索引是分配给元素的数字,也称为元素的位置。 索引以 0 开头。您可以访问列表或一系列条目中的一个条目。
W
init  
wizardforcel 已提交
15

W
wizardforcel 已提交
16
```py
W
init  
wizardforcel 已提交
17 18 19 20 21 22 23
>>> a = [1,2,3,4,5]
>>> a[2]
3
>>> a[1:3]
[2, 3]
```

W
wizardforcel 已提交
24
如果尝试访问未填充的索引(该索引不包含任何值),则会收到`IndexError`
W
init  
wizardforcel 已提交
25

W
wizardforcel 已提交
26
```py
W
init  
wizardforcel 已提交
27 28 29 30 31 32 33 34 35
>>> a = []
>>> a[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
```

但是,如果您对不包含索引的列表进行切片,就不会出现错误,而只是一个值比您期望的少的列表(或者当然是一个空列表):

W
wizardforcel 已提交
36
```py
W
init  
wizardforcel 已提交
37 38 39 40 41 42 43 44 45
>>> a = [1,2,3,4,5]
>>> a[:7]
[1, 2, 3, 4, 5]
>>> a[6:7]
[]
>>> a[8:]
[]
```

W
wizardforcel 已提交
46
### 将新元素添加到 python 列表
W
init  
wizardforcel 已提交
47 48 49

大多数情况下,您没有可用于构建列表的预定义值。 因此,必须在列表中添加元素。

W
wizardforcel 已提交
50
有很多方法。 一种是使用`append`函数,该函数将**一个元素**附加到列表的末尾。 请注意,这是一个要素。 为什么? 您将很快在示例中看到。
W
init  
wizardforcel 已提交
51 52 53

第二种方法是使用切片。 这样,您可以将新元素添加到列表中(或替换当前存在的元素)。 在这种情况下,您必须使用列表作为操作的右侧。 同样,**使用此方法时要小心**,因为如果从错误的位置进行切片,最终可能会丢失当前内容。

W
wizardforcel 已提交
54
而且,如果要将列表附加到列表的末尾,则必须使用需要将可迭代对象作为参数的`extend`函数-否则可以使用连接。 连接与我们在字符串中学到的相同:加号(`+`)。
W
init  
wizardforcel 已提交
55

W
wizardforcel 已提交
56
将元素插入列表开头的最佳方法是对`[:0]`使用切片。 这会将元素插入列表的开头。
W
init  
wizardforcel 已提交
57

W
wizardforcel 已提交
58
如果必须在给定索引处将元素插入列表中的某处,请使用具有两个参数的列表`insert`函数:插入位置的索引和要插入的对象。 如果索引被某个值占用,则新对象将在给定索引处插入此值之前。 如果未占用索引,则将对象附加到列表。
W
init  
wizardforcel 已提交
59

W
wizardforcel 已提交
60
```py
W
init  
wizardforcel 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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
>>> a = []
>>> a[0] = 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.append(2)
>>> a
[2]
>>> a[1:2] = [1]
>>> a
[2, 1]
>>> a[1:3] = [1]
>>> a
[2, 1]
>>> a[1:2] = [3,4,5,6]
>>> a
[2, 3, 4, 5, 6]
>>> a[:] = [2]
>>> a
[2]
>>> a[0:] = [1]
>>> a
[1]
>>> a.extend([2,3,4,5])
>>> a
[1, 2, 3, 4, 5]
>>> a.append([6,7,8])
>>> a
[1, 2, 3, 4, 5, [6, 7, 8]]
>>> a[:0] = [9]
>>> a
[9, 1, 2, 3, 4, 5, [6, 7, 8]]
>>> a[:0] = [10,11,12]
>>> a
[10, 11, 12, 9, 1, 2, 3, 4, 5, [6, 7, 8]]
>>> a += [13,14,15]
>>> a
[10, 11, 12, 9, 1, 2, 3, 4, 5, [6, 7, 8], 13, 14, 15]
>>> a = []
>>> a.insert(2,1)
>>> a
[1]
>>> a.insert(2,2)
>>> a
[1, 2]
>>> a.insert(2,3)
>>> a
[1, 2, 3]
>>> a.insert(2,4)
>>> a
[1, 2, 4, 3]
```

W
wizardforcel 已提交
114
如您所见,将列表附加到列表会在末尾插入整个列表,而不是所提供列表的值。 因此,在这种情况下,请小心并使用扩展。
W
init  
wizardforcel 已提交
115

W
wizardforcel 已提交
116
### 更新 python 列表中的元素
W
init  
wizardforcel 已提交
117

W
wizardforcel 已提交
118
如您所知,您可以通过列表中的元素的索引来更新它。 再次:如果您尝试访问当前未填充的索引,则会收到`IndexError`
W
init  
wizardforcel 已提交
119

W
wizardforcel 已提交
120
```py
W
init  
wizardforcel 已提交
121 122 123 124 125 126 127 128 129 130 131 132
>>> a = ["Java", "is", "cool!"]
>>> a
['Java', 'is', 'cool!']
>>> a[0] = 'Python'
>>> a
['Python', 'is', 'cool!']
```

### 从列表中删除元素

有时您需要从列表中删除一个元素,因为不再需要它,或者您只是在该位置放置了错误的元素。 还有一些方法可以从列表中删除元素。

W
wizardforcel 已提交
133
首先,您可以使用`del`语句。 由于它适用于变量,因此适用于整个列表(因为它们是变量)和列表元素。 *请记住*,如果您删除列表变量,则在不重新分配的情况下将无法访问该列表。
W
init  
wizardforcel 已提交
134

W
wizardforcel 已提交
135
另一种方法是从列表中弹出一个元素。 `pop`函数采用一个可选参数,该参数是要删除的索引。 如果不提供此参数,则最后一个元素将被删除。 `del``pop`之间用法的核心区别在于`pop`返回已删除的元素,因此您可以实际使用它。
W
init  
wizardforcel 已提交
136

W
wizardforcel 已提交
137
```py
W
init  
wizardforcel 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151
>>> a = [1, 2, 3, 4, 5, [6, 7, 8]]
>>> a
[1, 2, 3, 4, 5, [6, 7, 8]]
>>> del a[5]
>>> a
[1, 2, 3, 4, 5]
>>> a.pop(3)
4
>>> a.pop()
5
>>> a
[1, 2, 3]
```

W
wizardforcel 已提交
152
如果要从列表中删除所有元素,可以使用新的空列表重新分配列表,或使用列表的`clear`函数。
W
init  
wizardforcel 已提交
153

W
wizardforcel 已提交
154
这两种方法的区别在于,第一种方法确实在内存中留下了一些占用空间,并且列表的 ID 发生了变化,而清除列表则保留了 ID(内存位置)。
W
init  
wizardforcel 已提交
155

W
wizardforcel 已提交
156
```py
W
init  
wizardforcel 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
>>> a = [1, 2, 3, 4, 5, [6, 7, 8]]
>>> id(a)
4335030344
>>> a = []
>>> id(a)
4334935560
>>> b = [1, 2, 3, 4, 5, [6, 7, 8]]
>>> id(b)
4335073672
>>> b.clear()
>>> id(b)
4335073672
```

### 其他基本列表操作

您要在编程中使用的列表上的另外两个基本运算符是确定列表的长度,并使用给定的值初始化给定长度的列表。

第一个问题是众所周知的,第二个问题在进行动态编程或要从一种使用初始化的编程语言中迁移算法时最常见。

W
wizardforcel 已提交
177
```py
W
init  
wizardforcel 已提交
178 179 180 181 182 183 184 185 186 187
>>> a = [1, 2, 3, 4, 5]
>>> len(a) # returns the length of the given list
5
>>> a = ["Hello!"]*4 # creates a list with the element repeated 4 times
>>> a
['Hello!', 'Hello!', 'Hello!', 'Hello!']
```

So what next? Lets Learn [Python 3 Set](https://javabeginnerstutorial.com/python-tutorial/python-3-set-2/). 🙂