Lists · 列表
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| list/lɪst/ | 列表 | liè biǎo |
| elements/ˈelɪmənts/ | 元素 | yuán sù |
| ordered collection/ˈɔːdəd kəˈlekʃn/ | 有序集合 | yǒu xù jí hé |
| index/ˈɪndeks/ | 索引 | suǒ yǐn |
| traverse/trəˈvɜːs/ | 遍历 | biàn lì |
| general/ˈdʒenərəl/ | 通用 | tōng yòng |
| append/əˈpend/ | 追加 | zhuī jiā |
A list holds many elements
- A list 列表 stores an ordered collection 有序集合 of values, called elements 元素, under one name.
- A single list
coloursmight hold "red", "green", and "blue". - One name replaces many separate variables.
- The order is fixed, so each element has a definite position.
列表容纳许多元素
- 一个列表(list)把一个有序集合(ordered collection)的值,叫元素(elements),存在一个名字下。
- 一个列表
colours可能装 "red"、"green" 和 "blue"。 - 一个名字取代许多分开的变量。
- 顺序是固定的,所以每个元素有一个确定的位置。
A list stores: · 一个列表存储:
Many values, one name, in a fixed order. · 许多值,一个名字,固定顺序。
Access by index
- You access or change an element using its index 索引 — its position in the list.
- In many languages the first element is at index $0$:
colours[0]is "red". - Read
colours[1]to get "green", or assign tocolours[2]to change it. - Always check where a language starts counting.
按索引访问
- 你用索引(index,它在列表中的位置)访问或改变一个元素。
- 在许多语言中第一个元素在索引 $0$:
colours[0]是 "red"。 - 读
colours[1]得 "green",或给colours[2]赋值来改变它。 - 总要检查一门语言从哪里开始计数。
Access an element, or change the list? · 访问一个元素,还是改变列表?
Reading colours[1] accesses an element; append, insert, and remove change the list's contents. · 读 colours[1] 访问一个元素;追加、插入和移除改变列表的内容。
If lists start at index 0, colours[0] in ["red","green","blue"] is: · 如果列表从索引 0 开始,["red","green","blue"] 中 colours[0] 是:
Index 0 is the first element, "red". · 索引 0 是第一个元素,"red"。
Visiting every element of a list in turn with a loop is called ______ the list. · 用一个循环依次访问列表的每个元素叫______列表。
Traversal processes the whole list, whatever its size. · 遍历处理整个列表,无论它的大小。
Traverse with a loop
- To process a whole list, use iteration to traverse 遍历 it.
- A loop visits every element in turn — first, second, third, and so on.
- The same loop works no matter how many elements the list holds.
- This is what makes list code so general 通用.
用循环遍历
- 要处理整个列表,用迭代来遍历(traverse)它。
- 一个循环依次访问每个元素——第一个、第二个、第三个,等等。
- 无论列表装多少个元素,同样的循环都有效。
- 这就是让列表代码如此通用(general)的原因。
The append operation: · 追加操作:
Append grows the list by one at the end. · 追加在末尾把列表增长一个。
Traversing [70, 85, 90] and adding each to a total gives what final total? · 遍历 [70, 85, 90] 并把每个加到总和,最终总和是多少?
70 + 85 + 90 = 245. · 70 + 85 + 90 = 245。
The same traversal loop works whether the list has 3 elements or 300. · 无论列表有 3 个元素还是 300 个,同样的遍历循环都有效。
The loop refers to the list by name, making it general. · 循环按名字引用列表,使它通用。
Change the contents
- Lists support operations that change their contents:
- append 追加 adds a new element to the end;
- insert adds an element at a chosen position; remove takes one out.
Total a list. scores holds [70, 85, 90]. Start total ← 0, then traverse: 70 → total 70; 85 → total 155; 90 → total 245. Final total 245. Append a fourth score and the same loop adds it too — no code change.
改变内容
- 列表支持改变其内容的操作:
- 追加(append)在末尾加一个新元素;
- 插入在选定位置加一个元素;移除取出一个。
求列表总和。 scores 装 [70, 85, 90]。开始 total ← 0,然后遍历:70 → total 70;85 → total 155;90 → total 245。最终 total 245。追加第四个分数,同样的循环也加它——不改代码。
A list is an ordered collection of elements under one name, reached by index (often from 0). Traverse it with a loop to process every element, which keeps code general for any size. Lists also change: append, insert, and remove.
一个列表是一个名字下的有序集合的元素,通过索引(常从 0)访问。用一个循环遍历它来处理每个元素,这让代码对任何大小都通用。列表也会改变:追加、插入和移除。