Appearance
3.10 Lists 列表
The way statements are sequenced and combined in a program determines the computed result. Programs incorporate iteration and selection constructs to represent repetition and make decisions to handle varied input values.
- 程序中语句的排序和组合方式决定了计算结果。程序结合迭代和选择结构来表示重复并做出决策来处理各种输入值。
核心要点 Core Points
The exam reference sheet provides basic operations on lists, including:
- 考试参考表提供列表的基本操作,包括:
- Accessing an element by index:
aList[i]accesses the element ofaListat indexi. The first element ofaListis at index 1 and is accessed using the notationaList[1].- 通过索引访问元素:
aList[i]访问aList在索引i处的元素。aList的第一个元素在索引1处,使用符号aList[1]访问。
- 通过索引访问元素:
- Assigning a value of an element of a list to a variable:
x ← aList[i]assigns the value ofaList[i]to the variablex.- 将列表元素的值赋给变量:
x ← aList[i]将aList[i]的值赋给变量x。
- 将列表元素的值赋给变量:
- Assigning a value to an element of a list:
aList[i] ← xassigns the value ofxtoaList[i].- 将值赋给列表元素:
aList[i] ← x将x的值赋给aList[i]。
- 将值赋给列表元素:
List assignment:
aList[i] ← aList[j]assigns the value of aList[j] to aList[i].- 列表赋值:
aList[i] ← aList[j]将aList[j]的值赋给aList[i]。
- 列表赋值:
Inserting elements at a given index:
INSERT (aList, i, value)shifts to the right any values in aList at indices greater than or equal to i. The length of the list is increased by 1, and value is placed at index i in aList.- 在给定索引处插入元素:
INSERT (aList, i, value)将aList中索引大于或等于i的任何值向右移动。列表长度增加1,value被放置在aList的索引i处。
- 在给定索引处插入元素:
Adding elements to the end of the list:
APPEND(aList, value)increases the length of aList by 1, and value is placed at the end of aList.- 向列表末尾添加元素:
APPEND(aList, value)将aList的长度增加1,value被放置在aList的末尾。
- 向列表末尾添加元素:
Removing elements:
REMOVE (aList, i)removes the item at index i in aList and shifts to the left any values at indices greater than i. The length of aList is decreased by 1.- 删除元素:
REMOVE (aList, i)删除aList中索引i处的项目,并将索引大于i的任何值向左移动。aList的长度减少1。
- 删除元素:
Determining the length of a list:
LENGTH(aList)evaluates to the number of elements currently in aList.- 确定列表长度:
LENGTH(aList)计算为aList中当前元素的数量。
- 确定列表长度:
List procedures are implemented in accordance with the syntax rules of the programming language.
- 列表过程根据编程语言的语法规则实现。
List traversal is either a complete traversal (all elements accessed) or a partial traversal (only a portion of elements accessed).
- 列表遍历要么是完全遍历(访问所有元素),要么是部分遍历(只访问部分元素)。
Iteration statements can be used to traverse a list.
- 迭代语句可用于遍历列表。
The exam reference sheet provides the syntax for iteration:
- 考试参考表提供迭代语法:
- Text:
- 文本:
FOR EACH item IN aList { <block of statements> } - Block:
- 块:
FOR EACH item IN aList [block of statements]
The variable
itemis assigned the value of each element ofaListsequentially, from the first to the last element. The code within theblock of statementsis executed once for each assignment ofitem.- 变量
item被顺序分配aList中每个元素的值,从第一个到最后一个元素。block of statements中的代码对item的每次赋值执行一次。
- 变量
Knowledge of existing algorithms that use iteration can aid in constructing new algorithms. Examples of such algorithms often used with lists include:
- 使用迭代的现有算法的知识有助于构建新算法。经常与列表一起使用的此类算法的例子包括:
- Determining a minimum or maximum value in a list.
- 确定列表中的最小值或最大值。
- Computing a sum or average of a list of numbers.
- 计算数字列表的总和或平均值。
Linear search or sequential search algorithms check each element of a list, in order, until the desired value is found or all elements in the list have been checked.
- 线性搜索或顺序搜索算法按顺序检查列表的每个元素,直到找到所需值或检查完列表中的所有元素。
学生活动 Student Activities
For list operations:
- 对于列表操作:
- Write expressions that use list indexing and list procedures.
- 编写使用列表索引和列表过程的表达式。
- Evaluate expressions that use list indexing and list procedures.
- 计算使用列表索引和列表过程的表达式。
For algorithms involving elements of a list:
- 对于涉及列表元素的算法:
- Write iteration statements to traverse a list.
- 编写迭代语句来遍历列表。
- Determine the result of an algorithm that includes list traversals.
- 确定包含列表遍历的算法的结果。
