字典,續篇

Python 中級

Hugo Bowne-Anderson

Data Scientist at DataCamp

重點整理

world = {"afghanistan":30.55, "albania":2.77, "algeria":39.21}

world["albania"]
2.77
world = {"afghanistan":30.55, "albania":2.77, 
         "algeria":39.21, "albania":2.81}

world
{'afghanistan': 30.55, 'albania': 2.81, 'algeria': 39.21}
Python 中級

重點整理

  • 鍵必須是「不可變」物件。
{0:"hello", True:"dear", "two":"world"}
{0: 'hello', True: 'dear', 'two': 'world'}
{["just", "to", "test"]: "value"}
TypeError: unhashable type: 'list'
Python 中級

錫蘭王國(Sealand)

ch2_2_slides.015.png

1 來源:Wikipedia
Python 中級

Dictionary(字典)

world["sealand"] = 0.000027

world
{'afghanistan': 30.55, 'albania': 2.81, 
        'algeria': 39.21, 'sealand': 2.7e-05}
"sealand" in world
True
Python 中級

Dictionary(字典)

world["sealand"] = 0.000028

world
{'afghanistan': 30.55, 'albania': 2.81, 
        'algeria': 39.21, 'sealand': 2.8e-05}
del(world["sealand"])

world
{'afghanistan': 30.55, 'albania': 2.81, 'algeria': 39.21}
Python 中級

List vs. Dictionary

Python 中級

List vs. Dictionary

Python 中級

List vs. Dictionary

List Dictionary
[] 選取、更新、刪除 [] 選取、更新、刪除
Python 中級

List vs. Dictionary

List Dictionary
[] 選取、更新、刪除 [] 選取、更新、刪除
Python 中級

List vs. Dictionary

List Dictionary
[] 選取、更新、刪除 [] 選取、更新、刪除
以連續數值作索引
Python 中級

List vs. Dictionary

List Dictionary
[] 選取、更新、刪除 [] 選取、更新、刪除
以連續數值作索引 以唯一鍵作索引
Python 中級

List vs. Dictionary

List Dictionary
[] 選取、更新、刪除 [] 選取、更新、刪除
以連續數值作索引 以唯一鍵作索引
值的集合——順序重要,可用來選整段子集
Python 中級

List vs. Dictionary

List Dictionary
[] 選取、更新、刪除 [] 選取、更新、刪除
以連續數值作索引 以唯一鍵作索引
值的集合——順序重要,可用來選整段子集 以唯一鍵查找的對照表
Python 中級

一起來練習吧!

Python 中級

Preparing Video For Download...