1 Star 0 Fork 136

孔祥/e0501-builtins

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
e03_dict_quiz.py 16.72 KB
一键复制 编辑 原始数据 按行查看 历史
kkkkx 提交于 2023-11-05 20:24 . done
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
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
new_data = {}
for key, value in dct.items():
new_key = key.upper()
new_data[new_key] = float(
format(log(value), ".4f")
) ### format保留四位小数后输出的是字符串,所以需要float转成浮点数
return new_data
# 完成以下函数, 使用字典推导式进行转换, 键保持不变, 但值列表要取其平均数,
# 保留 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]:
new_dct = {}
for key, value in dct.items():
new_key = key
new_dct[new_key] = float(format(sum(value) / len(value), ".1f"))
return new_dct
# 完成以下函数, 使用字典推导式进行转换, 将 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
dct = {}
for element in lst:
a = sqrt(element[0] ** 2 + element[1] ** 2)
if a < 1:
dct[element] = float(format(a, ".2f"))
return dct
# 完成以下函数, 将 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]:
a = zip(keys, vals)
dct_t = list(a)
dct = dict(dct_t)
return dct
# 完成以下函数, 将 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]]:
a = zip(keys, cols)
dct_t = list(a)
dct = dict(dct_t)
return dct
# 完成以下函数, 将 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]]:
a = zip(*rows)
b = list(a)
new_rows = []
for i in b:
new_rows.append(list(i)) ##append就是直接添加,不能用res = res.append()
zipper = zip(keys, new_rows)
dct = dict(zipper)
return dct
# 完成以下函数, 将 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]]:
my_lst = []
for i in rows:
a = zip(keys, i)
b = dict(a)
my_lst.append(b)
return my_lst
# 完成以下函数, 将 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]]:
lst = []
zipper = zip(*cols)
my_lst = list(zipper)
for i in my_lst:
lst.append(list(i))
llst = []
for i in lst:
dct = dict(zip(keys, i))
llst.append(dct)
return llst
# 完成以下函数, 实现从列表字典 (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]]:
dct = {}
for i in lst:
for keys, element in i.items():
new_keys = keys
dct[new_keys] = None
for j, k in dct.items():
a = []
for i in range(len(lst)):
a.append(lst[i][j])
dct[j] = a
return dct
# 完成以下函数, 实现从列表字典 (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]:
a = {}
i = 0
p_lst = []
while i >= 0:
for keys, lst in dct.items():
new_keys = keys
a[new_keys] = lst[i]
b = a.copy() ###不copy的话p_lst.append()后,列表中的字典会随着原字典的改变而改变
p_lst.append(b)
if i < len(lst) - 1:
i += 1
else:
break
return p_lst
# 完成以下函数, 使函数不返回值 (返回 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:
a = {newk: newv}
res = dct.update(a)
return 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:
res = dct.copy()
a = {newk: newv}
res.update(a)
return res
# 完成以下函数, 使函数不修改 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:
a = {newk: newv}
for k, v in dct.items():
a[k] = v
return a
# 完成以下函数, 使函数不返回值 (返回 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:
if key in dct:
del dct[key]
return None
else:
del dct[key]
return KeyError
# 完成以下函数, 从 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:
if key in dct:
a = dct[key]
del dct[key]
return a
else:
del dct[key]
return KeyError
# 完成以下函数, 从 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:
if key in dct:
a = dct[key]
del dct[key]
return a
else:
return 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:
if akey in dct:
return dct[akey]
else:
dct[akey] = aval
return dct[akey]
# 完成以下函数, 使函数不返回值 (返回 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.update(**kvs)
return 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:
a = dct.copy()
a.update(**kvs)
return a
# 完成以下函数, 修改 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:
a = dct1.copy()
b = dct2.copy()
dct1.clear()
dct2.clear()
dct1.update(b)
dct2.update(a)
return 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:
if key in dct:
return dct[key]
else:
del dct[key]
return KeyError
# 完成以下函数, 返回 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:
if key in dct:
return dct[key]
else:
return None
# 完成以下函数, 从若干个提供的字典里, 依次查找 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:
for i in dcts:
if key in i:
return i[key]
else:
return None
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/kong-xiang123/e0501-builtins.git
git@gitee.com:kong-xiang123/e0501-builtins.git
kong-xiang123
e0501-builtins
e0501-builtins
master

搜索帮助