Python 内置了字典: dict 的支持, dict 全称 dictionary, 在其他语言中也称为 map, 字典是另 一种可变容器模型, 且可存储任意类型对象具有极快的查找速度
字典是一种通过名字或者关键字引用的得数据结构, 其键可以是数字字符串元组, 这种结构类型也称之为映射字典类型是 Python 中唯一內建的映射类型, 基本的操作包括如下:
(1) len() : 返回字典中 键: 值对 的数量
(2) d[k] : 返回关键字对应的值;
(3) d[k] = v : 将值关联到键值 k 上;
(4) del d[k ] : 删除键值为 k 的项;
(5) key in d : 键值 key 是否在 d 中, 是返回 True, 否则返回 False
举例字典:
- d = {
- "k1": 18,
- "k2": True,
- "k3": [Su, {
- kk1: vv1,
- kk2: vv2,
- kk3: (11, 22),
- }
- ],
- "k4": (11, 22, 33, 44)
- }
1. 字典的基本格式:
d = {key1 : value1, key2 : value2 }
2. 字典的键 (key) 必须不可变 (如: 字符串, 数字, 元组), 值(key) 可以为任意数据类型
- d1 = {Alice: 2341, 9102: Bibi, (11,22): 3258}# 正确
- d2 = {[Alice,123]: 2341, True: 9102, {abc: 123, efg: 456}}# 错误! 因为列表 布尔值 字典都是可变的, 不能作为键(key)
3. 字典是无序的, 每次执行打印, 顺序都会发生改变
- d3 = {Alice: 2341, 9102: Bibi, (11,22): 3258}
- print(d)
- # 打印结果可能是这样
- {Alice: 2341, 9102: Bibi, (11, 22): 3258}
- # 也有可能是这样
- {(11, 22): 3258, Alice: 2341, 9102: Bibi}
- # 甚至可能是这样
- {9102: Bibi, Alice: 2341, (11, 22): 3258}
- ...
- ...
4. 根据键 (key) 或值 (value) 取出对应的值 (value) 或键(key) & for 循环
1. 返回关键字对应的值: v = d[key] 例(参考举最上方举例字典):
- v = d["k1"]# 取出 k1 对应的 value
- v1 = d["k3"][1]["kk3"][0]# 取出 kk3 对应的 value 中的 11
- print(v, v1)
2. 不可通过切片方式获取键值对, 因为字典无序
3.for 循环取 key 和 value
- d4 = {Year: 2018, Month: 3, Day: 18}
- # 循环所有的 key (或这么写: for item in d4.keys():)
- for item in d4:
- print(item)
打印结果:
- Year
- Month
- Day
- # 循环所有的 value
- for item in d4.values():
- print(item)
打印结果:
- 2018
- 3
- 18
- # 循环所有的 key:value
- for item in d4.keys():
- print(item, :, d4[item])
- for k, v in d4.items(): # 用 items 方法 k 接收 key ,v 接收 value
- print(k, v)
5. 字典的函数 & 方法
1. 函数(3):
- 1.str(dict)# 输出字典可以打印的字符串表示
- d6 = {Year: 2018, Month: 3, Day: 18}
- content = str(d6)
- print(content)
打印结果:{Year: 2018, Month: 3, Day: 18}
- 2.len(dict)# 计算字典内元素的个人, 即键的总数
- d7 = {Year: 2018, Month: 3, Day: 18}
- count = len(d7)
- print(count)
打印结果: 3
- 3.type(variable)# 返回输入的变量类型, 如果变量类型是字典就返回字典
- d8 = {Year: 2018, Month: 3, Day: 18}
- ty = type(d8)
- print(ty)
打印结果:<class dict>
2. 方法()
1. 删除
- d4 = {Alice: 2341, 9102: Bibi, (11,22): 3258}
- del d4[9102] # 删除元素 9102:Bibi
- print(d4) # 打印结果:{Alice: 2341, (11, 22): 3258}
- d4.clear() # 清空字典
- print(d4) # 打印结果:{}
- del d4 # 删除字典
- 2.items
- d4 = {Year: 2018, Month: 3, Day: 18}
- for k, v in d4.items(): # 用 items 方法 k 接收 key ,v 接收 value
- print(k, v)
- 3.keys
- d4 = {Year: 2018, Month: 3, Day: 18}
- for item in d4.keys(): # 循环所有的 key
- print(item)
- 4.values
- d4 = {Year: 2018, Month: 3, Day: 18}
- for item in d4.values(): # 循环所有的 values
- print(item)
- 5.copy(浅拷贝)
- d4 = {Year: 2018, Month: 3, Day: 18}
- dict2 = d4.copy()
- print ("新复制的字典为 :",dict2)
6.fromkeys() 创建一个新字典, 以序列 seq 中元素做字典的键, val 为字典所有键对应的初始值
- seq = (Year, Month, Day)
- dict1 = dict.fromkeys(seq)
- print("新的字典 : %s" % str(dict1))
- dict1 = dict1.fromkeys(seq, 8)
- print("新的字典 : %s" % str(dict1))
- # 输出结果:
- # 新的字典 : {Year: None, Month: None, Day: None}
- # 新的字典 : {Year: 8, Month: 8, Day: 8}
来源: http://www.bubuko.com/infodetail-2530414.html