Lists and data abstraction · 列表与数据抽象
Why a list?
- A program often needs many values at once, like a class of scores.
- One variable per value would be messy.
- A list holds many values under one name — a useful data abstraction.
为什么用列表?
- 程序常常一次需要很多个值,比如全班的分数。
- 一个值用一个变量会很乱。
- 列表(list)用一个名字保存很多个值 —— 这是一个有用的数据抽象(data abstraction)。
What is a data abstraction?
- An abstraction hides detail so you can think about the whole, not the parts.
- A list lets you treat 1 value or 1000 values the same way.
- You write the same code no matter how long the list is.
什么是数据抽象?
- 抽象(abstraction)隐藏细节,让你思考整体,而不是每个部分。
- 列表让你用同样的方式处理 1 个值或 1000 个值。
- 不管列表有多长,你写的代码都一样。
Make a list and read it
- Write items inside square brackets:
[10, 20, 30]. - Get one item by its index (its position). Python starts at
0. scores[0]is the first item;scores[-1]is the last item.
创建列表并读取它
- 把元素写在方括号里:
[10, 20, 30]。 - 用索引(index,也就是位置)取出一个元素。Python 从
0开始。 scores[0]是第一个元素;scores[-1]是最后一个元素。
scores = [10, 20, 30]
print(scores[0])
print(scores[-1])
print(len(scores))
Grow a list with append
scores.append(x)addsxto the end of the list.- The list can grow while the program runs.
len(scores)tells you how many items it now has.
用 append 让列表变长
scores.append(x)把x加到列表的末尾。- 程序运行时,列表可以变长。
len(scores)告诉你现在有多少个元素。
scores = [10, 20]
scores.append(30)
print(scores)
print(len(scores))
Traverse (visit every item)
- To traverse a list means to go through every item, one by one.
for s in scores:gives you one item each pass — this is a for-each loop.- It is the clean way to process the whole list.
遍历(访问每一个元素)
- 遍历(traverse)一个列表,就是一个接一个地走过每个元素。
for s in scores:每一轮给你一个元素 —— 这是 for-each(逐项)循环。- 这是处理整个列表的简洁方法。
scores = [10, 20, 30]
total = 0
for s in scores:
total = total + s
print(total)
In AP CSP pseudocode
- A list is the exam's list too — but CB lists are 1-indexed.
- So
aList[1]is the first element in CB, while Python's first isscores[0]. FOR EACH x IN listmatches Python'sfor x in list:.DISPLAY= print,←==.
用 AP CSP 伪代码表示
- 列表也是考试里的 list —— 但 CB 的列表是从 1 开始计数的(1-indexed)。
- 所以在 CB 里
aList[1]是第一个元素,而 Python 的第一个是scores[0]。 FOR EACH x IN list对应 Python 的for x in list:。DISPLAY= print,←==。
aList ← [10, 20, 30]
DISPLAY aList[1] ## the FIRST item (1-indexed!)
total ← 0
FOR EACH x IN aList
{
total ← total + x
}
DISPLAY total
Common mistakes
- Index a list from
0; the last item isa[-1]. - A list groups related values under one name.
常见错误
- 列表从
0开始索引;最后一个元素是a[-1]。 - 列表把相关的值归到一个名字下。
Now you try
- Build, grow, or traverse a list in each task.
- Press Check answer to test your code.
现在轮到你
- 在每个任务里创建、扩展或遍历一个列表。
- 按检查答案来测试你的代码。
A list of items, indexed · 有索引的列表
A list stores many values in order, each at an index from 0. · 列表按顺序存很多值,每个都有从 0 开始的索引。
Start from the list colors = ["red", "green"]. Use .append(...) to add "blue" to the end, so the list becomes ["red", "green", "blue"]. · 从列表 colors = ["red", "green"] 开始。用 .append(...) 把 "blue" 加到末尾,使列表变成 ["red", "green", "blue"]。
Click Run to see the output here. · 点击“运行”查看此处输出。
nums is [4, 8, 15, 16, 23]. Traverse it with a for loop to add up every value, then print the total. (It should print 66.) · nums 是 [4, 8, 15, 16, 23]。用 for 循环遍历它,把每个值加起来,然后 print 出总和。(应打印 66。)
Click Run to see the output here. · 点击“运行”查看此处输出。
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]. Store the first · 第一个 day in first and the last day in last using indexing. · days = ["Mon", "Tue", "Wed", "Thu", "Fri"]。用索引把第一个工作日存进 first,把最后一个存进 last。
Click Run to see the output here. · 点击“运行”查看此处输出。