So the correct answer is the second one. So hopefully, no one picked
the first answer. We usually do try to do things the easy way.
There is sort of an exception to that in this whole unit. That
we're learning ourselves how to define a hash table. Even though Python provides
a dictionary type, which I'll talk about at the very end of the
unit. That makes things much easier than what we're doing ourselves, but we
really want to understand a lot of computer science by building our own hash
table, and then we won't actually need to use it because there's a built-in
text that works much better. But the answer is
the second answer, and here's the reason why. So what
happens when we evaluate this list multiplication, well, we have
this empty list, let's draw it like this. And we
create a new list, which is three copies of that
list. But it's not copies, it's three references to it.
So here's the new list. It has three elements. Each
one of those elements refers to the same empty list.
Then when we did table index 1 append, well, what
happened is we follow this reference to whatever table index
1 is, this object we appended to it, added a
new element. But because the references at position zero, one, and
two, all refer to the same object, that changed all
of those values, not just the value of table index 1.
And we can see that if we print out the
whole table, that the table contains three elements. But they're all
the same. And they all refer to the same object.
So any change we make to one changes all of them.
正解は2つ目です
1つ目の答えを選んだ人がいないことを望みます
普通は簡単な方法を試みます
このレッスン全体は一種の例外です
Pythonがディクショナリ型を提供しているのに
私たちはハッシュテーブルの定義方法を学んでいます
これについてはレッスンの最後にお話ししますが
私たちのやり方よりはるかに簡単です
ですが自分でハッシュテーブルを作成することで
コンピュータサイエンスを理解する必要があります
実際に使用する必要はありません
はるかに機能がよい組み込みテキストがあります
正解は2つ目の選択肢です
その理由を説明しましょう
このリストの乗算を評価する時に起きることです
空のリストがあります
このように書いてみましょう
新しいリストを作成します
このリストの3つのコピーです
ですが実はコピーではありません 3つの参照です
3つの要素を含む新しいリストです
それぞれの要素が同じ空のリストを参照しています
そしてtable[1]にappendを行うと起きることは
table[1]が何であれtable[1]の参照に従います
このオブジェクトはappendされ
新しい要素を追加しました
しかし位置がゼロ、1、2の位置の参照は
すべて同じオブジェクトを参照しているため
すべての値が変わりました
table[1]の値だけではありません
table全体を表示します
tableが3つの要素を含んでいることが確認できます
ですがこれはすべて同じです
すべて同じオブジェクトを参照しています
ですから1つに行った変更が
すべての要素を変更します