加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
e03_dict_quiz.py 13.72 KB
一键复制 编辑 原始数据 按行查看 历史
from typing import List, Dict, Tuple, Any
# 完成以下函数, 使用字典推导式进行转换, 将键变为大写,
# 将值取自然对数, 保留 4 位小数, 比如
#
# f001({"open": 45.70, "high": 47.00, "low": 45.49, "close": 46.83}) == {
# "OPEN": 3.8221, "HIGH": 3.8501, "LOW": 3.8175, "CLOSE": 3.8465
# }
#
# f001({"open": 6.61, "high": 6.63, "low": 6.45, "close": 6.48}) == {
# "OPEN": 1.8886, "HIGH": 1.8916, "LOW": 1.8641, "CLOSE": 1.8687
# }
#
# 提示: 可以使用 round 内置函数
def f001(dct: Dict[str, float]) -> Dict[str, float]:
from math import log
...
# 完成以下函数, 使用字典推导式进行转换, 键保持不变, 但值列表要取其平均数,
# 保留 1 位小数, 比如
#
# f002({"a": [3, 9], "b": [2, 2, 7, 7], "c": [5]}) == {
# "a": 6.0, "b": 4.5, "c": 5.0
# }
#
# f002({"d": [1, 1, 1], "e": [3, 2, 3, 3], "f": [0, 0]}) == {
# "d": 1.0, "e": 2.8, "f": .0
# }
#
def f002(dct: Dict[str, List[int]]) -> Dict[str, float]:
...
# 完成以下函数, 使用字典推导式进行转换, 将 lst 列表里的元素作为键,
# 元素的向量长度 (保留 2 位小数) 作为值
# (向量长度为每个分量的平方和再取开方),
# 然后只保留长度小于 1 (单位圆半径内) 的那些向量, 比如
#
# f003([(0.3, 0.4), (0.8, -0.8), (0.9, 0.4)]) == {
# (0.3, 0.4): 0.5, (0.9, 0.4): 0.98
# }
# f003([(-0.5, -0.5), (-1, 1), (-0.2, 0.9), (0.7, 0.7)]) == {
# (-0.5, -0.5): 0.71, (-0.2, 0.9): 0.92, (0.7, 0.7): 0.99
# }
#
# 提示: 可以使用 sum, round 等内置函数, 以及赋值表达式
def f003(lst: List[Tuple[float, float]]) -> Dict[Tuple, float]:
from math import sqrt
...
# 完成以下函数, 将 keys 列表和 vals 列表组成一个字典,
# 其中 keys 的元素作为键, values 的元素作为值, 比如
#
# my_keys = ["aaa", "bbb", "ccc"]
# my_vals = [5.982, 4.221, 9.475]
# f004(my_keys, my_vals) == {
# "aaa": 5.982, "bbb": 4.221, "ccc": 9.475,
# }
#
# my_keys = ["ddd", "eee", "fff", "ggg"]
# my_vals = [5.982, 4.221, 9.475, 6.685]
# f004(my_keys, my_vals) == {
# "ddd": 5.982, "eee": 4.221, "fff": 9.475, "ggg": 6.685
# }
#
# 提示: 可以使用 zip, dict 内置函数
def f004(keys: List[str], vals: List[float]) -> Dict[str, float]:
...
# 完成以下函数, 将 keys 列表里的元素作为键, cols 列表列表 (list of list)
# 里的元素作为列值 (column values), 组成一个列表字典 (dict of list),
# 由于字典里储存的是列列表, 故被称为列式数据集 (column-wise dataset), 比如
#
# my_keys = ["aaa", "bbb", "ccc"]
# my_cols = [
# [5.982, 2.904],
# [4.221, 3.434],
# [9.475, 6.549],
# ]
# f005(my_keys, my_cols) == {
# "aaa": [5.982, 2.904],
# "bbb": [4.221, 3.434],
# "ccc": [9.475, 6.549],
# }
#
# my_keys = ["ddd", "eee", "fff", "ggg"]
# my_cols = [
# [5.982, 8.853, 5.923],
# [4.221, 3.123, 8.121],
# [9.475, 6.659, 7.492],
# [6.685, 4.129, 2.223],
# ]
# f005(my_keys, my_cols) == {
# "ddd": [5.982, 8.853, 5.923],
# "eee": [4.221, 3.123, 8.121],
# "fff": [9.475, 6.659, 7.492],
# "ggg": [6.685, 4.129, 2.223],
# }
#
# 提示: 可以使用 zip, dict 内置函数
def f005(keys: List[str], cols: List[List[float]]) -> Dict[str, List[float]]:
...
# 完成以下函数, 将 keys 列表里的元素作为键, rows 列表列表 (list of list)
# 里的元素作为行值 (row values), 组成一个列表字典 (dict of list),
# 由于字典里储存的是列列表, 故被称为列式数据集 (column-wise dataset), 比如
#
# my_keys = ["aaa", "bbb", "ccc"]
# my_rows = [
# [5.982, 4.221, 9.475],
# [2.904, 3.434, 6.549],
# ]
# f006(my_keys, my_rows) == {
# "aaa": [5.982, 2.904],
# "bbb": [4.221, 3.434],
# "ccc": [9.475, 6.549],
# }
#
# my_keys = ["ddd", "eee", "fff", "ggg"]
# my_rows = [
# [5.982, 4.221, 9.475, 6.685],
# [8.853, 3.123, 6.659, 4.129],
# [5.923, 8.121, 7.492, 2.223],
# ]
# f006(my_keys, my_rows) == {
# "ddd": [5.982, 8.853, 5.923],
# "eee": [4.221, 3.123, 8.121],
# "fff": [9.475, 6.659, 7.492],
# "ggg": [6.685, 4.129, 2.223],
# }
#
# 提示: 需要做类似转置的变换, 可以参考 e02_list_quiz.f021
def f006(keys: List[str], rows: List[List[float]]) -> Dict[str, List[float]]:
...
# 完成以下函数, 将 keys 列表里的元素作为键, rows 列表列表 (list of list)
# 里的元素作为行值 (row values), 组成一个字典列表 (list of dict),
# 由于列表里储存的是行字典, 故被称为行式数据集 (row-wise dataset), 比如
#
# my_keys = ["aaa", "bbb", "ccc"]
# my_rows = [
# [5.982, 4.221, 9.475],
# [2.904, 3.434, 6.549],
# ]
# f007(my_keys, my_rows) == [
# {"aaa": 5.982, "bbb": 4.221, "ccc": 9.475},
# {"aaa": 2.904, "bbb": 3.434, "ccc": 6.549},
# ]
#
# my_keys = ["ddd", "eee", "fff", "ggg"]
# my_rows = [
# [5.982, 4.221, 9.475, 6.685],
# [8.853, 3.123, 6.659, 4.129],
# [5.923, 8.121, 7.492, 2.223],
# ]
# f007(my_keys, my_rows) == [
# {"dddd": 5.982, "eee": 4.221, "fff": 9.475, "ggg": 6.685},
# {"dddd": 8.853, "eee": 3.123, "fff": 6.659, "ggg": 4.129},
# {"dddd": 5.923, "eee": 8.121, "fff": 7.492, "ggg": 2.223},
# ]
#
def f007(keys: List[str], rows: List[List[float]]) -> List[Dict[str, float]]:
...
# 完成以下函数, 将 keys 列表里的元素作为键, cols 列表列表 (list of list)
# 里的元素作为列值 (column values), 组成一个字典列表 (list of dict),
# 由于列表里储存的是行字典, 故被称为行式数据集 (row-wise dataset), 比如
#
# my_keys = ["aaa", "bbb", "ccc"]
# my_cols = [
# [5.982, 2.904],
# [4.221, 3.434],
# [9.475, 6.549],
# ]
# f008(my_keys, my_cols) == [
# {"aaa": 5.982, "bbb": 4.221, "ccc": 9.475},
# {"aaa": 2.904, "bbb": 3.434, "ccc": 6.549},
# ]
#
# my_keys = ["ddd", "eee", "fff", "ggg"]
# my_cols = [
# [5.982, 8.853, 5.923],
# [4.221, 3.123, 8.121],
# [9.475, 6.659, 7.492],
# [6.685, 4.129, 2.223],
# ]
# f008(my_keys, my_cols) == [
# {"dddd": 5.982, "eee": 4.221, "fff": 9.475, "ggg": 6.685},
# {"dddd": 8.853, "eee": 3.123, "fff": 6.659, "ggg": 4.129},
# {"dddd": 5.923, "eee": 8.121, "fff": 7.492, "ggg": 2.223},
# ]
#
# 提示: 需要做类似转置的变换, 可以参考 e02_list_quiz.f021
def f008(keys: List[str], cols: List[List[float]]) -> List[Dict[str, float]]:
...
# 完成以下函数, 实现从列表字典 (dict of list) (列式数据集 column-wise dataset)
# 到字典列表 (list of dict) (行式数据集 row-wise data) 的转置 (transpose),
# 比如
#
# row_ds == [
# {"aaa": 4.287, "bbb": 9.821, "ccc": 1.934},
# {"aaa": 2.194, "bbb": 5.338, "ccc": 6.574},
# ]
# f009(row_ds) = {
# "aaa": [4.287, 2.194],
# "bbb": [9.821, 5.338],
# "ccc": [1.934, 6.574],
# }
#
# row_ds == [
# {"ddd": 8.764, "eee": 4.329},
# {"ddd": 0.926, "eee": 3.927},
# {"ddd": 5.551, "eee": 7.452},
# ]
# f009(row_ds) = {
# "ddd": [8.764, 0.926, 5.551],
# "eee": [4.329, 3.927, 7.452],
# }
#
# 提示: 推导式套推导式, 一行代码足矣
def f009(lst: List[Dict[str, float]]) -> Dict[str, List[float]]:
...
# 完成以下函数, 实现从列表字典 (dict of list) (列式数据集 column-wise dataset)
# 到字典列表 (list of dict) (行式数据集 row-wise data) 的转置 (transpose),
# 比如
#
# col_ds = {
# "aaa": [4.287, 2.194],
# "bbb": [9.821, 5.338],
# "ccc": [1.934, 6.574],
# }
# f010(col_ds) == [
# {"aaa": 4.287, "bbb": 9.821, "ccc": 1.934},
# {"aaa": 2.194, "bbb": 5.338, "ccc": 6.574},
# ]
#
# col_ds = {
# "ddd": [8.764, 0.926, 5.551],
# "eee": [4.329, 3.927, 7.452],
# }
# f010(col_ds) == [
# {"ddd": 8.764, "eee": 4.329},
# {"ddd": 0.926, "eee": 3.927},
# {"ddd": 5.551, "eee": 7.452},
# ]
#
# 提示: 综合使用推导式、解包表达式以及 dict, zip, dict.keys, dict.values
# 等内置类型/函数/方法
def f010(dct: Dict[str, List]) -> List[Dict]:
...
# 完成以下函数, 使函数不返回值 (返回 None), 但在 dct 字典的最后新增一对
# 键值对, 键是 newk, 值是 newv, 比如
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f011(my_dct, "d", 5)
# res is None
# my_dct == {"a": 3, "b": 1, "c": 2, "d": 5}
#
# my_dct = {"e": 3.14, "f": 2.74}
# res = f011(my_dct, "g", 0.25)
# res is None
# my_dct == {"e": 3.14, "f": 2.74, "g": 0.25}
#
def f011(dct: Dict, newk: str, newv: Any) -> None:
...
# 完成以下函数, 使函数不修改 dct 字典, 而是在 dct 字典的基础上返回一个
# 新的字典实例, 在字典的最后面新增一对键值对, 键是 newk, 值是 newv, 比如
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f012(my_dct, "d", 5)
# res == {"a": 3, "b": 1, "c": 2, "d": 5}
#
# my_dct = {"e": 3.14, "f": 2.74}
# res = f012(my_dct, "g", 0.25)
# res == {"e": 3.14, "f": 2.74, "g": 0.25}
#
def f012(dct: Dict, newk: str, newv: Any) -> Dict:
...
# 完成以下函数, 使函数不修改 dct 字典, 而是在 dct 字典的基础上返回一个
# 新的字典实例, 在字典的最前面新增一对键值对, 键是 newk, 值是 newv, 比如
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f013(my_dct, "d", 5)
# res == {"d": 5, "a": 3, "b": 1, "c": 2}
#
# my_dct = {"e": 3.14, "f": 2.74}
# res = f013(my_dct, "g", 0.25)
# res == {"g": 0.25, "e": 3.14, "f": 2.74}
#
def f013(dct: Dict, newk: str, newv: Any) -> Dict:
...
# 完成以下函数, 使函数不返回值 (返回 None), 但要从 dct 字典里删除掉
# 键 key 所对应的键值对, 如果 key 在 dct 字典里不存在, 那么要报告
# KeyError 异常, 比如
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f014(my_dct, "b")
# res is None
# my_dct == {"a": 3, "c": 2}
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f014(my_dct, "d") # KeyError
#
def f014(dct: Dict, key: str) -> None:
...
# 完成以下函数, 从 dct 字典里删除掉键 key 所对应的键值对, 返回键 key 所对应的
# 值, 如果 key 在 dct 字典里不存在, 那么要报告 KeyError 异常, 比如
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f015(my_dct, "b")
# res == 1
# my_dct == {"a": 3, "c": 2}
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f015(my_dct, "d") # KeyError
#
def f015(dct: Dict, key: str) -> None:
...
# 完成以下函数, 从 dct 字典里删除掉键 key 所对应的键值对, 返回键 key 所对应的
# 值, 如果 key 在 dct 字典里不存在, 那么返回 None, 比如
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f016(my_dct, "b")
# res == 1
# my_dct == {"a": 3, "c": 2}
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f016(my_dct, "d")
# res is None
#
def f016(dct: Dict, key: str) -> None:
...
# 完成以下函数, 使得: 若 dct 字典里存在键 akey, 则不对 dct 做任何改变;
# 若 dct 字典里不存在键 akey, 则在 dct 字典的最后新增一对键值对,
# 键是 akey, 值是 aval, 最后, 函数的返回值是 akey 键所对应的值, 比如
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f017(my_dct, "c", 5)
# res == 2 # NOT 5
# my_dct = {"a": 3, "b": 1, "c": 2}
#
# my_dct = {"a": 3, "c": 2}
# res = f017(my_dct, "b", 5)
# res == 5
# my_dct = {"a": 3, "c": 2, "b: 5"}
#
def f017(dct: Dict, akey: str, aval: Any) -> Any:
...
# 完成以下函数, 使函数不返回值 (返回 None), 使用其传入的任意命名实参
# 来更新 dct 字典里的键值对, 比如
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f018(my_dct, a=5, c=4)
# res is None
# my_dct = {"a": 5, "b": 1, "c": 4}
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f018(my_dct, a=5, e=4, d=0)
# res is None
# my_dct = {"a": 5, "b": 1, "c": 2, "e": 4, "d": 0}
#
def f018(dct: Dict, **kvs) -> None:
...
# 完成以下函数, 使函数返回一个新的字典实例, 该字典以 dct 字典为基础,
# 使用其传入的任意命名实参来更新键值对, 比如
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f019(my_dct, a=5, c=4)
# res = {"a": 5, "b": 1, "c": 4}
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f019(my_dct, a=5, e=4, d=0)
# res = {"a": 5, "b": 1, "c": 2, "e": 4, "d": 0}
#
def f019(dct: Dict, **kvs) -> Dict:
...
# 完成以下函数, 修改 dct1 和 dct2 两字典实例, 使其内容相互对换,
# 不返回值 (返回 None), 比如
#
# my_dct1 = {"a": 3, "b": 1, "c": 2}
# my_dct2 = {"d": 5, "e": 4}
# res = f020(my_dct1, my_dct2)
# res is None
# my_dct1 = {"d": 5, "e": 4}
# my_dct2 = {"a": 3, "b": 1, "c": 2}
#
# my_dct1 = {"d": 5, "c": 1}
# my_dct2 = {"b": 3, "e": 4, "a": 2}
# res = f020(my_dct1, my_dct2)
# res is None
# my_dct1 = {"b": 3, "e": 4, "a": 2}
# my_dct2 = {"d": 5, "c": 1}
#
def f020(dct1: Dict, dct2: Dict) -> None:
...
# 完成以下函数, 返回 dct 字典里 key 键所对应的值, 如果 key 键在 dct 字典里
# 不存在, 则报告 KeyError 异常, 比如
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f021(my_dct, "c")
# res == 2
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f021(my_dct, "e") # KeyError
#
def f021(dct: Dict[str, Any], key: str) -> Any:
...
# 完成以下函数, 返回 dct 字典里 key 键所对应的值, 如果 key 键在 dct 字典里
# 不存在, 则返回 None, 比如
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f021(my_dct, "c")
# res == 2
#
# my_dct = {"a": 3, "b": 1, "c": 2}
# res = f021(my_dct, "e")
# res is None
#
def f022(dct: Dict[str, Any], key: str) -> Any:
...
# 完成以下函数, 从若干个提供的字典里, 依次查找 key 键所对应的值,
# 将首个查找到的值作为函数的返回值, 如果 key 键在所有提供的字典里都不存在,
# 则返回 None, 比如
#
# dct1 = {"a": 3, "b": 1, "c": 2}
# dct2 = {"c": 5, "d": 4}
# res = f023("c", dct1, dct2)
# res == 2
# res = f023("c", dct2, dct1)
# res == 5
# res = f023("d", dct1, dct2)
# res == 4
# res = f023("e", dct1, dct2)
# res is None
# res = f023("e", dct1, dct2, {"e": 7})
# res == 7
# res = f023("c")
# res is None
#
# 提示: 可以考虑构造一个融合字典, 从中 get
def f023(key: str, *dcts: Tuple[Dict[str, Any]]) -> Any:
...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化