Skip to content

Lists

Python Basics Lesson 9 2:48 English narration · English + 中文 subtitles burned in

space play · ←/→ 5s · j/l 10s · f fullscreen · ,/. speed

Chapters

Transcript
Five test scores, and so far you would give them a name each: score one, score two, and so on. 五个考试分数,按你目前学的做法,会给每一个取一个名字:score1、score2,等等。
It works until you need to add them up, and then you are writing every name out by hand — and it falls apart completely the day there are thirty. 在你要把它们加起来之前,这样还行得通;到那时你就得把每个名字都手写一遍—— 而等到有 30 个分数时,这个办法就彻底垮了。
Put them in square brackets instead, separated by commas, and one name holds all five. 改成把它们放进方括号里,用逗号隔开,一个名字就装下了全部五个。
You reach an item exactly the way you reached a character in a string. 取出其中一项的方法,和你在字符串里取一个字符完全一样。
The first item is at index zero — ten, here. 第一项在索引 0——这里是 10。
The last is at minus one, which is thirty, and that works no matter how long the list is. 最后一项在 -1,也就是 30, 不管列表有多长都成立。
But a list can do something a string cannot: you can put a new value in. 但列表能做一件字符串做不到的事:你可以放进新的值。
Assign to nums, square brackets zero, and the ten becomes ninety-nine. 给 nums 赋值,10 就变成了 99。
The list itself changed. 列表本身被改变了。
A list does not have a fixed size. 列表的大小不是固定的。
Dot append adds one item to the end, right while the program is running — which is how you collect answers, readings or names as they arrive, without knowing in advance how many there will be. .append() 会在程序运行的过程中,往末尾添加一项—— 你就是这样在事先不知道数量的情况下,把答案、读数或名字一个个收集起来的。
And len tells you how many items are in there now: three before the append, four after it. len 则告诉你现在里面有多少项:append 之前是 3,之后是 4。
Now the two ideas meet. 现在两个概念碰到一起了。
For n in nums walks through the list one item at a time, and on each pass n holds the value — not the index, the value itself. for n in nums 会一项一项地走过这个列表, 每一轮 n 里装的是那个值——不是索引,是值本身。
So no square brackets are needed inside the loop. 所以循环里面不需要写方括号。
Add the accumulator you learned in lesson seven and you have a running total: four, twelve, twenty-seven, forty-three, sixty-six. 再加上你在第 7 课学的累加器,就得到一个running total: 4、12、27、43、66。
Three lines total any list of any length. 三行代码,能求出任意长度列表的总和。
Now the lesson's third task: find the largest value. 现在做课程里的第三道题:找出最大的值。
The pattern is the same as the accumulator, with one important change — start it at the first item, not at zero. 套路和累加器一样,但有一个重要的改动——要从第一项开始,而不是从 0 开始。
Start at zero and a list of negative numbers gives you zero, which is not even in the list. 如果从 0 开始,一个全是负数的列表会给出 0,而 0 根本不在列表里。
Then walk through, and whenever a value is bigger than the one you have, keep the new one. 然后一项项走过去,只要某个值比你手上的那个大,就换成新的。
Four, eight, fifteen, sixteen, and finally twenty-three. 4、8、15、16,最后是 23。
Four things to take with you. 带走四点。
One: square brackets hold many values under one name — this is the exam's one-dimensional array. 第一:方括号把多个值放在一个名字下面——这就是考试里的一维数组。
Two: the first item is index zero and the last is minus one, exactly as in a string. 第二:第一项是索引 0,最后一项是 -1,和字符串完全一样。
Three: append adds one item to the end, and len counts them. 第三:append 在末尾添加一项,len 数出有多少项。
Four: for item in list gives you the value on each pass, so you rarely need an index at all. 第四:for item in list 每一轮给你的是值,所以你很少真的需要索引。
Now do the three tasks. 现在去做那三道题。

Log in or create account

IGCSE, A-Level & AP