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.
リストとは?
- プログラムは同時に多数の値を必要とすることが多く、クラス全体の得点のような場合があります。
- 各値ごとに1つの変数を設定すると、整理が乱れます。
- リストは1つの名前の下に多数の値を保持し、有用なデータ抽象化を提供します。
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.
データ抽象化とは何ですか?
- 抽象化は詳細を隠し、個々の部分ではなく全体について考えることを可能にします。
- リストを使うことで、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]。 - インデックス(位置)によって1つの項目を取得します。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.
トラバース(すべての項目を訪問する)
- リストをトラバースすることは、1つずつすべての項目を通ることです。
for s in scores:は各パスで1つの項目を提供 — これは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擬似コード
- リストは試験のリストでもありますが、CBのリストは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]です。 - リストは関連する値を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. · 実行ボタンをクリックして出力を確認してください。