For the final quiz for this unit, your goal is to change the look up procedure
to now work with dictionaries.
Before we had look up working where index was a list of entries,
and we did the look up by looping through the index, that required a lot of work
we had to go through each entry, check if the keyword matched,
and then return the one that matched, if we got to the end without finding it,
we returned none.
Your goal for the quiz, is to modify this code
to now work where the index is going to be the dictionary,
and we should be able to find the entry much more quickly.
We don't need to loop through anything.
we can use the property of the dictionary to look up the entry right away.
If you understand the add to index code that we just did,
I think you can define look up on your own.
The one thing to be careful about,
is that you need make sure that when the index does not contain the keyword,
when it's not in the dictionary,
instead of producing an error, that it returns the value none.
このレッスンの最後の小テストです
目標はディクショナリで機能するよう
lookupの関数を変更することです
エントリのリストでlookupを行う前に
インデックスをループすることでlookupを行っており
たくさんの作業が必要でした
各エントリを1つずつ調べなくてはなりませんでしたね
キーワードの一致をチェックし
一致したものを返しました
最後まで見つけられなければNoneを返しました
この小テストでのあなたの目標は
インデックスがディクショナリになっても
機能するようにこれらのコード修正することです
そしてさらに速く
エントリを見つけ出さなければなりません
何もループする必要はありません
すぐにエントリをlookupするために
ディクショナリのプロパティを使用します
先ほど行ったように
インデックスコードへの追加が理解できていれば
皆さんは自分でlookupを定義できると思います
気をつけなければいけないことは
インデックスにキーワードが含まれていない場合も
想定する必要があるということです
キーワードがディクショナリにない場合は
エラーを発生させるのではなく値Noneを返します
本单元最后一次小测验 是要修改look up过程
让它可以查找一个字典
在将索引变为一个条目链表之前
完成look up要循环所有索引 过程十分复杂
先要检查每个条目中的关键词是否匹配
再返回匹配的一个
如果没找到 就无法得到返回结果
这次小测验的要求是修改这个代码
索引是字典
应该能更快找到条目
不需要循环遍历
可以用字典的特性 来立即查找条目
如果理解了add to index的代码
就能自己定义look up了
要注意一点
就是要确保索引不包含关键词
它没在字典里的时候
它会返回一个空键值 而不是出现错误
本單元的最後一個測驗,目標是要修改 lookup 程序
使它能與字典一起運作
之前 lookup 操作的索引,是項目列表
完成查找,要循環走過整個索引,需要大量的工作
要檢查每個項目中的關鍵字是否符合
然後傳回符合的項目,如果直到最後還是沒找到
就傳回 None
這個測驗的目標是修改這個程式碼
現在操作的索引是字典
我們應該能更快地找到項目
不需要再做循環
可以用字典的特性,立刻找到項目
如果你理解剛才的 add-to-index 程式碼
我認為你可以自行定義 lookup
要注意一件事
就是要確保索引不包含關鍵字時
關鍵字不在字典裡的時候
要傳回 None,而不是產生 error