不間斷 Python 挑戰 Day 15 - 更多關於字典 (Dictionary)

2021/12/23閱讀時間約 10 分鐘
前面的文章中我們學習了關於字典的基本用法,今天再討論更多關於字典的其它用法,以及它和串列、元組等的關聯。

使用for迴圈迭代

  • 字典可以使用for迴圈迭代,最簡單的語法如下,須注意此處取得的是字典的鍵:
cities_dict = { "台北": 2531659,
         "新北": 4011586,
         "桃園": 2272452,
         "台中": 2814422,
         "台南": 1863435,
         "高雄": 2746939}

for city in cities_dict:
  print(city, end=" ")
執行結果:
台北 新北 桃園 台中 台南 高雄
  • 明確一點,若要取得字典的鍵可以使用字典的keys()方法:
for city in cities_dict.keys():
  print(city, end=" ")
執行結果和前面相同:
台北 新北 桃園 台中 台南 高雄
  • 若想取得的是字典的值,可以使用字典的values()方法:
for population in cities_dict.values():
  print(population, end=" ")
執行結果:
2531659 4011586 2272452 2814422 1863435 2746939
  • 也可以鍵和值同時取出,使用的是字典的items()方法,字典的鍵將傳給city、字典的值將傳給population:
for city, population in cities_dict.items():
  print(city, population, end=" ")
執行結果:
台北 2531659 新北 4011586 桃園 2272452 台中 2814422 台南 1863435 高雄 2746939
  • 實際上,items()方法傳回的資料型態是元組,上述方法只是用兩個變數分別去存取元組的內容:
for city_pop in cities_dict.items():
  print(city_pop, end=" ")
執行結果:
('台北', 2531659) ('新北', 4011586) ('桃園', 2272452) ('台中', 2814422) ('台南', 1863435) ('高雄', 2746939)

串列元素為字典

串列內的元素可以是字典,在以上的例子中,當每個城市除了人口數之外,需要記錄的特徵變多了,例如城市的面積,我們可以把每個城市的資訊建成一個字典,再把所有的字典放入一個串列中管理。
city0 = { "name": "台北", "population": 2531659, "area": 271}
city1 = { "name": "新北", "population": 4011586, "area": 2052}
city2 = { "name": "桃園", "population": 2272452, "area": 1220}
city3 = { "name": "台中", "population": 2814422, "area": 2214}
city4 = { "name": "台南", "population": 1863435, "area": 2191}
city5 = { "name": "高雄", "population": 2746939, "area": 2951}
special_municipality_list = [city0, city1, city2, city3, city4, city5]
for city in special_municipality_list:
  print(city)
執行結果:
{'name': '台北', 'population': 2531659, 'area': 271}
{'name': '新北', 'population': 4011586, 'area': 2052}
{'name': '桃園', 'population': 2272452, 'area': 1220}
{'name': '台中', 'population': 2814422, 'area': 2214}
{'name': '台南', 'population': 1863435, 'area': 2191}
{'name': '高雄', 'population': 2746939, 'area': 2951}

字典的值為串列

字典內的值可以為串列,如以下例子分別列舉了亞洲、歐洲與美洲做為字典的鍵,各大洲的一些國家分別儲存在陣列,做為字典的值,搭配字典的items()方法可以將其內容取出。
countries_dict = { "Asia": ["Japan", "Korea", "Taiwan"],
           "Europe": ["Germany", "France", "Spain"],
           "America": ["USA", "Canada", "Argentina"]}
for continent, country in countries_dict.items():
  print(f"{continent}: {country}")
執行結果:
Asia: ['Japan', 'Korea', 'Taiwan']
Europe: ['Germany', 'France', 'Spain']
America: ['USA', 'Canada', 'Argentina']

字典的值為字典

字典內的值也可以為字典,我們改寫前面字典串列的例子,把城市名稱做為鍵、城市的資訊做為值存入字典,再搭配字典的items()方法將其內容取出。
cities_dict = { "台北": {"population": 2531659, "area": 271},
         "新北": {"population": 4011586, "area": 2052},
         "桃園": {"population": 2272452, "area": 1220},
         "台中": {"population": 2814422, "area": 2214},
         "台南": {"population": 1863435, "area": 2191},
         "高雄": {"population": 2746939, "area": 2951}}
for city, info in cities_dict.items():
  print(f"{city}: {info}")
執行結果:
Asia: ('Japan', 'Korea', 'Taiwan')
Europe: ('Germany', 'France', 'Spain')
America: ('USA', 'Canada', 'Argentina')

程式範例

為什麼會看到廣告
Wei-Jie Weng
Wei-Jie Weng
留言0
查看全部
發表第一個留言支持創作者!