字典,第 2 部分

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 中级

锡兰公国

锡兰公国

1 来源:维基百科
Python 中级

字典

world["sealand"] = 0.000027

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

字典

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 中级

列表 vs. 字典

Python 中级

列表 vs. 字典

Python 中级

列表 vs. 字典

列表 字典
[] 选择、更新和删除 [] 选择、更新和删除
Python 中级

列表 vs. 字典

列表 字典
[] 选择、更新和删除 [] 选择、更新和删除
Python 中级

列表 vs. 字典

列表 字典
[] 选择、更新和删除 [] 选择、更新和删除
用连续数字索引
Python 中级

列表 vs. 字典

列表 字典
[] 选择、更新和删除 [] 选择、更新和删除
用连续数字索引 用唯一键索引
Python 中级

列表 vs. 字典

列表 字典
[] 选择、更新和删除 [] 选择、更新和删除
用连续数字索引 用唯一键索引
值的集合——顺序重要,便于选取整段子集
Python 中级

列表 vs. 字典

列表 字典
[] 选择、更新和删除 [] 选择、更新和删除
用连续数字索引 用唯一键索引
值的集合——顺序重要,便于选取整段子集 以唯一键查找的表
Python 中级

Passons à la pratique !

Python 中级

Preparing Video For Download...