建立 dictionary
在指令碼中又提供了 countries 和 capitals 兩個列表。你的任務是把這些資料轉成一個 dictionary,其中國家名稱作為鍵(keys),首都作為對應的值(values)。快速複習一下,以下是建立 dictionary 的範例:
my_dict = {
"key1":"value1",
"key2":"value2",
}
在這個範例裡,鍵和值都是字串。本題也是同樣的情況。
本練習屬於課程
Python 中級
練習說明
- 使用
countries和capitals中的字串,建立一個名為europe的 dictionary,包含 4 組 key:value 配對。請注意大小寫一致!所有地方都要使用小寫字母。 - 列印
europe,確認結果是否符合預期。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Definition of countries and capital
countries = ['spain', 'france', 'germany', 'norway']
capitals = ['madrid', 'paris', 'berlin', 'oslo']
# From string in countries and capitals, create dictionary europe
europe = {'spain':'madrid', ___, ___, ___ }
# Print europe