Lists (1-D arrays) · 列表(一维数组)
A list holds many values
- A list keeps many values in one variable.
- Write the items inside square brackets:
[10, 20, 30]. - The items can be numbers, strings, or a mix.
列表能存放很多值
- 列表(list)在一个变量里存放很多值。
- 把这些项写在方括号里:
[10, 20, 30]。 - 这些项可以是数字、字符串,或两者混合。
Get and change items
- Use an index, just like a string:
nums[0]is the first item. nums[-1]is the last item.- You can also change an item:
nums[0] = 99.
获取和修改元素
- 用索引,就像字符串一样:
nums[0]是第一个元素。 nums[-1]是最后一个元素。- 你也可以修改某个元素:
nums[0] = 99。
fruits = ["apple", "pear", "plum"]
print(fruits[0])
print(fruits[-1])
Add items and count them
nums.append(x)addsxto the end of the list.len(nums)tells you how many items there are.- A list can grow as your program runs.
添加元素并计数
nums.append(x)会把x加到列表的末尾。len(nums)会告诉你有多少个元素。- 列表可以在程序运行时不断增长。
fruits = ["apple", "pear"]
fruits.append("plum")
print(len(fruits))
print(fruits)
Loop over a list
for item in nums:visits each item in turn.- The loop variable holds one item per pass.
- This is the easy way to process every value.
遍历列表
for item in nums:会依次访问每个元素。- 循环变量每一轮持有一个元素。
- 这是处理每个值的简便方法。
scores = [10, 20, 30]
total = 0
for s in scores:
total = total + s
print(total)
In Cambridge pseudocode
- A list is the exam's 1-D array. Note: exam arrays often start at index
1, but Python lists start at0.
用剑桥伪代码表示
- 列表就是考试里的一维数组(1-D array)。注意:考试里的数组常常从索引
1开始,而 Python 列表从0开始。
DECLARE Scores : ARRAY[1:3] OF INTEGER
Scores[1] ← 10
Scores[2] ← 20
Scores[3] ← 30
FOR I ← 1 TO 3
OUTPUT Scores[I]
NEXT I
Common mistakes
- The first item is
a[0]and the last isa[-1];a[len(a)]is out of range. b = adoes not copy the list — both names share it. Usea.copy()to make a real copy.appendadds one item to the end.
常见错误
- 第一个元素是
a[0],最后一个是a[-1];a[len(a)]会越界。 b = a不会复制列表——两个名字共享它。要真复制用a.copy()。append在末尾加一个元素。
Now you try
- Build or loop over a list in each task.
- Press Check answer to test your code.
现在轮到你
- 每个任务都要构建或遍历一个列表。
- 按检查答案来测试你的代码。
A list changes in place · 列表会原地改变
A list is ordered and indexed; append/change/pop all modify it. · 列表是有序、有索引的;append/change/pop 都会修改它。
Create a list colors holding "red", "green", "blue". Then append "yellow" so the list has four items. · 创建一个列表 colors,包含 "red"、"green"、"blue"。然后 append 一个 "yellow",让列表有四个元素。
Click Run to see the output here. · 点击“运行”查看此处输出。
Loop over the list nums and add up its values. Store the total in total (the answer is 66). · 遍历列表 nums,把它的值加起来。把总和存进 total(答案是 66)。
Click Run to see the output here. · 点击“运行”查看此处输出。
Find the largest value in nums with a loop. Start largest at the first item, then loop through nums and keep the bigger value. Store the answer in largest (it is 23). · 用循环找出 nums 中最大的值。把 largest 设为第一个元素,然后遍历 nums,保留更大的值。把答案存进 largest(它是 23)。
Click Run to see the output here. · 点击“运行”查看此处输出。
Build a new · 新的 list backwards with the items of nums in reverse order. Use a loop, and do not change nums itself. · 新建一个列表 backwards,里面是 nums 的元素,但顺序倒过来。用循环完成,并且不要改变 nums 本身。
Click Run to see the output here. · 点击“运行”查看此处输出。