If I were allowed to take just one book to the proverbial desert island, it might be a dictionary.
― Steven Pinker
1. 前言
在这个生活中处处都是大数据和人工智能的时代,总是能在各种角落看到 Python 的培训广告。我招架不住敌方的猛烈攻势,败下阵来。经过了一分钟的深思熟虑,我决定利用我的三分钟热情进行回击,从零开始自学 Python。
2.
Dictionaries and Structuring Data
字典和数据结构化
2.1
The Dictionary Data Type
字典数据类型
Like a list, a dictionary is a mutable collection of many values.
像列表一样,字典是由多个值组成的可变集合。
Indexes for dictionaries are called keys, and a key with its associated value is called a key-value pair. Almost any type of value can be used as a dictionary key in Python, e.g. string, integer, float, Boolean objects and tuple. But a dictionary key must be of a type that is immutable. Therefor neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable.
字典的索引称为键,键和它所关联的值并称为键值对。在 Python 中几乎所有类型的值都可以用作字典的键,例如字符串,整数,浮点数,布尔对象和元组。 但是字典的键必须是不可变的类型。 因此,列表和其他字典都不能用作字典的键,因为列表和字典是可变的。
a = {
2: 'Ms.',
'name': 'Sherry'
}
print(a[2] + ' ' + a['name'])
# => Ms. Sherry
网友评论