1. 字典的定义
定义 : 字典是一种可变的、无序的、键值对的、复杂的数据容器
Python 中的字典是 Python 中一个键值映射的数据结构。
字典用 { } 定义
字典使用 键值对 存储数据,键值对之间使用 “,” 分隔
- 键 key 是索引
- 值 value 是数据
- 键和值之间使用 : 分隔
- 键必须是唯一的
- 值可以取任何数据类型,但键只能使用字符串、数字或元组,列表不可以(列表是可变的数据类型)
dict1 = {
"name" : "小明",
"age" : 18,
"gender" : True,
"height" : 1.75
}
2. 字典的常用操作
① 使用字典 [’ 键’] 可以取到字典里面的内容。
print (dict1['name'])
② 使用字典 [’ 键’] = 值 修改字典的内容。
dict1['height'] = 0
print(dict1['height'])
③ 当键不存在时添加内容。
dict1['weight'] = '小红'
print(dict1)
3. 字典的常用方法
方法 | 用法 |
---|---|
get(key, default=None) | 返回指定键的值,如果值不在字典中返回fault值 |
keys() | 以列表返回一个字典所有的键 |
values() | 以列表的形式返回字典所有值 |
update(dict2) | 把字典dict1的键/值对更新到 dict1 里 |
items() | 以列表返回可遍历的(键、值)元组数组 |
① 不清楚字典里有没有这个值,get(key, default=None)
print(dict1.get('name1')) # 没有整个值,返回 None
print(dict1.get('name')) # 有这个值,返回值,'小明'
print(dict1.get('name'),'未命名') # 有这个值,就返回值,'小明',没有的话返回我们设定的默认值
② 获取所有的键
dict1.keys()
#dict_keys(['name', 'age', 'gender', 'height'])
③ 获取所有的值
dict1.values()
# dict_values(['小明', 18, True, 1.75])
④ 获取所有的键值对
dict1.items()
# dict_items([('name', '小明'), ('age', 18), ('gender', True), ('height', 1.75)])
打印键值对的内容
for key in dict1.keys():
print(key,dict1[key])
#name 小明
#age 18
#gender True
#height 1.75
遍历所有的值,不能通过值获取键
for value in dict1.values():
print(value)
#小明
#18
#True
#1.75
打印键值对,得到的是元组
for item in dict1.items():
print(item)
#('name', '小明')
#('age', 18)
#('gender', True)
#('height', 1.75)
如果想得到的是元素,不是元组,可以这么操作
for key,value in dict1.items():
print(key,value)
#name 小明
#age 18
#gender True
#height 1.75
⑤ 将一个字典更新到另一个字典里
- 键重复:新的会覆盖旧的
- 键不存在:添加新的键值对
dict1 = {
"name" : "小明",
"age" : 18,
"gender" : True,
"height" : 1.75
}
dict2 = {
"name" : "小明",
"height" : 1.80,
"hobby" : ['吃']
}
dict1.update(dict2)
print(dict1)
# {'name': '小明', 'age': 18, 'gender': True, 'height': 1.8, 'hobby': ['吃']}
4. 案例:字符统计
统计一下段落的字符:
this_str = """In this paper, we present an extensive review on deep learning methods for image restoration tasks. Deep learning techniques, led by convolutional neural networks, have received a great deal of attention in almost all areas of image processing, especially in image classification. However, image restoration is a fundamental and challenging topic and plays significant roles in image processing, understanding and representation. It typically addresses image deblurring, denoising, dehazing, and super-resolution.
There are substantial differences in the approaches and mechanisms in deep learning methods for image restoration. Discriminative learning based methods are able to deal with issues of learning a restoration mapping function effectively, while optimisation models based methods can further enhance the performance with certain learning constraints. In this paper, we offer a comparative study of deep learning techniques in image denoising, deblurring, dehazing, and super-resolution, and summarise the principles
involved in these tasks from various supervised deep network architectures, residual or skip connection and receptive field to unsupervised autoencoder mechanisms. Image quality criteria are also reviewed and their roles in image restoration are assessed. Based on our analysis, we further present an efficient network for deblurring and a couple of multi-objective training functions for super-resolution restoration tasks. The proposed methods are compared extensively with the state-of-the-art methods with both quantitative and qualitative analyses. Finally, we point out potential challenges and directions for future research."""
任务分解:
4.1 统计一下哪些字符出现过
用键还是值统计出现过的字符?
使用出现过的字符充当字典的键,因为如果键已经存在会被覆盖掉,不存在添加键
d = {}
#打印了所有的字符
for char in this_str:
#print(char)
#使用出现过的字符充当字典的键
d[char] = 0
print(d)
print(d.keys())
#{'I': 0, 'n': 0, ' ': 0, 't': 0, 'h': 0, 'i': 0, 's': 0, 'p': 0, 'a': 0, 'e': 0, 'r': 0, ',': 0, 'w': 0, 'x': 0, 'v': 0, 'o': 0, 'd': 0, 'l': 0, 'g': 0, 'm': 0, 'f': 0, 'k': 0, '.': 0, 'D': 0, 'c': 0, 'q': 0, 'u': 0, 'b': 0, 'y': 0, 'H': 0, 'z': 0, '-': 0, '\n': 0, 'T': 0, 'B': 0, 'j': 0, 'F': 0}
#dict_keys(['I', 'n', ' ', 't', 'h', 'i', 's', 'p', 'a', 'e', 'r', ',', 'w', 'x', 'v', 'o', 'd', 'l', 'g', 'm', 'f', 'k', '.', 'D', 'c', 'q', 'u', 'b', 'y', 'H', 'z', '-', '\n', 'T', 'B', 'j', 'F'])
#把它转换为列表
print(list(d.keys()))
#['I', 'n', ' ', 't', 'h', 'i', 's', 'p', 'a', 'e', 'r', ',', 'w', 'x', 'v', 'o', 'd', 'l', 'g', 'm', 'f', 'k', '.', 'D', 'c', 'q', 'u', 'b', 'y', 'H', 'z', '-', '\n', 'T', 'B', 'j', 'F']
4.2 统计一下字符出现的次数
写一个判断,利用字典键出现过会覆盖的特点
d = {}
#打印了所有的字符
for char in this_str:
#print(char)
#使用出现过的字符充当字典的键
#需要统计一下每一个字符出现的次数
#第一次出现就记录一下
if char not in d.keys():
d[char] = 1
#第N次出现,就把字典的值+1
else:
d[char] +=1
print(d)
print(d.keys())
print(list(d.keys()))
#{'I': 4, 'n': 122, ' ': 222, 't': 104, 'h': 38, 'i': 125, 's': 101, 'p': 40, 'a': 122, 'e': 187, 'r': 101, ',': 18, 'w': 15, 'x': 2, 'v': 20, 'o': 87, 'd': 55, 'l': 55, 'g': 34, 'm': 33, 'f': 30, 'k': 7, '.': 11, 'D': 2, 'c': 42, 'q': 5, 'u': 39, 'b': 10, 'y': 12, 'H': 1, 'z': 2, '-': 7, '\n': 2, 'T': 2, 'B': 1, 'j': 1, 'F': 1}
#dict_keys(['I', 'n', ' ', 't', 'h', 'i', 's', 'p', 'a', 'e', 'r', ',', 'w', 'x', 'v', 'o', 'd', 'l', 'g', 'm', 'f', 'k', '.', 'D', 'c', 'q', 'u', 'b', 'y', 'H', 'z', '-', '\n', 'T', 'B', 'j', 'F'])
#['I', 'n', ' ', 't', 'h', 'i', 's', 'p', 'a', 'e', 'r', ',', 'w', 'x', 'v', 'o', 'd', 'l', 'g', 'm', 'f', 'k', '.', 'D', 'c', 'q', 'u', 'b', 'y', 'H', 'z', '-', '\n', 'T', 'B', 'j', 'F']