So now that we've built it ourselves, I'm going to show
you the easy way to do it, which is to
use the built in Python type called a dictionary. And
what a dictionary is is really an implementation of a hash
table. It's built into Python. So it's much easier to
use than defining our own hash table. So far, we've
seen two complex types in Python. We've seen the string
type and we've seen the list type. Now we're going to introduce
the dictionary type. So there are many things that are
common between these three. There are other things that are
different. So to create a string we could have a
sequence of characters inside quotes. To create a list, we
use the square bracket. And we could have a sequence
of elements inside the square bracket, and the elements could
be any type, unlike a string where they had to
be characters. So, our string was a sequence of characters.
Our list was a list of any kind of value. So, a list of elements of any value.
The dictionary type we're going to create using the curly
bracket. And the entries inside the dictionary are key
value pairs. So here I've created a dictionary with
two elements. And each element, the key is a
string. Here is the string hydrogen and the value
associated with that element is a number. The keys
in a dictionary can be any immutable value, so they don't
need to be strings, they could be numbers. They could be
other things. The values can be any value. So what a
dictionary is, is a set of key value pairs and the property
that a dictionary will give us is like the hash table,
that we can look up a key and get a value associated
with that key. So one important property of all types is
whether they're immutable or mutable. We saw that the string was immutable.
That means once we create a string, we can't
modify that string. It has the same value it always
has. A list was mutable, we could change the values
of elements in the list after we create the list.
We could append to the list, adding new elements
to it. A dictionary is also mutable. So after we
create a dictionary, we could add new key value pairs
to it. We could change the value of values associated
with keys. We saw with strings, if s is a string, then we can do s index i.
That will give us the ith letter in the
string. With lists, we can do something very similar. If
p is a list, we can do p index i to get the ith element of the list. With
dictionaries, it's a little bit different. The syntax is
the same. We can do d index k. With both
strings and lists, these were numbers. Here the k is
whatever the key value is. So this is a key
in the dictionary. What d index k will give us
the value associated with that key in the dictionary. We'll
see some examples soon. We can also use our indexing
and assignments. We saw, with the list, we could do
this. That would replace the value of the ith element
of p with whatever we have here. With the string,
we can't do that. That produces an error.
And the reason we can't do it is because
strings are immutable. So there's no way to
modify characters in a string. With the dictionary we
can. And what the meaning of this, is
very similar to the meaning of update in our
hash table. So that's equivalent to update the
value. The value associated with k to be v.
自分で作成できましたね
これから簡単な方法を紹介します
ディクショナリと呼ばれる
Pythonの組み込み型を使用します
ディクショナリとは
ハッシュテーブルの実装のことです
それがPythonに組み込まれています
ですからハッシュテーブルを
最初から定義するよりはるかに簡単です
これまでPythonで2つの複雑な型を学習しました
文字列型とリスト型を学習しました
今回はディクショナリ型を紹介します
この3つには共通点がたくさんありますが
異なる点もあります
文字列の作成では
クォーテーション内に一連の文字を置きましょう
またリストを作成では角括弧を使用します
角括弧内に一連の要素を置きましょう
そして文字でなければいけない文字列とは違い
要素はあらゆるものになり得ます
私たちの文字列は連続した文字でした
リストはあらゆる種類の値のリストでした
つまり任意の値の要素のリストです
ディクショナリ型は波括弧を使用して作成します
そしてディクショナリ内のエントリはキーと値の組です
ここに2つの要素でディクショナリを作成しました
それぞれの要素キーは文字列です
これが文字列hydrogenです
そしてその要素に関連する値は数値です
ディクショナリ内のキーは
あらゆる不変の値になることができます
ですから文字列になる必要はありません
数値になります
別のものにもなります 値は任意の値です
ディクショナリとは
キーと値の組とプロパティのセットで
ハッシュテーブルのような作用があります
キーをlookupでき
そのキーに関連する値を取得できます
すべての型における重要なプロパティの1つが
不変か可変かということで
文字列は不変であると学習しました
つまり一度作成した文字列を修正することはできません
常に同じ値を有しているのです
リストは可変でリスト作成後でも
リスト内の要素の値を変更できます
新しい要素をリストに追加するappendができます
ディクショナリも可変で
作成後に新たなキーと値の組を
追加することができます
キーに関連している値を変更することもできます
文字列で学習したように
もしsが文字列ならs[i]を行えます
これによって文字列内のi番目の文字を得られます
リストでもとてもよく似たことができます
pがリストならリストのi番目の要素を得るために
p[i]を行うことができます
ディクショナリでは少し異なります
構文は同じでd[k]を行うことができます
文字列とリストではこれは数値でした
このkはキー値になります
これがディクショナリのキーです
d[k]によって得られるものは
ディクショナリ内のそのキーに関連した値です
後ほどいくつか例を確認します
インデックスと代入も使用できます
リストで学習したようにこれも行えます
pのi番目の要素の値を
ここにあるもので置き換えられます
文字列ではエラーを発生させるので
それができませんでした
その理由は文字列が不変だからです
文字列の文字を変更する方法はありません
ディクショナリでは行えます
この理由はハッシュテーブルのupdateと
とてもよく似ています
つまり値を更新するのと同じことで
kに関連する値はvになります