代码拉取完成,页面将自动刷新
同步操作将从 首都经济贸易大学-高强/e0501-builtins 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
from typing import List, Any, Tuple
# 完成以下函数, 使用列表推导式进行选择和转换,
# 从 lst 列表里只保留严格大于零的元素, 然后对其取平方, 比如
#
# f001([2, 3, -1, 5, 0, 4]) == [4, 9, 25, 16]
# f001([1, -1, 2, -2, 3, -3]) == [1, 4, 9]
# f001([-2, -3, -1]) == []
#
def f001(lst: List[int]) -> List[int]:
return [i**2 for i in lst if i > 0]
# 完成以下函数, 使用列表推导式和内置函数 sum 统计字符 / 的数量, 比如
#
# f002(["/home", "/home/qiang", "/etc"]) == 4
# f002(["https://www.cueb.edu.cn", "www.baidu.com"]) == 2
# f002(["pystudy", "exercise"]) == 0
#
def f002(lst: List[str]) -> int:
return sum(s.count('/') for s in lst)
# 完成以下函数, 使用列表推导式和内置函数 range、print, 使得
#
# f003(3) prints
# L
# LL
# LLL
#
# f003(5) prints
# L
# LL
# LLL
# LLLL
# LLLLL
#
# 提示: print 若运用灵活, 本题一行代码即可实现
def f003(n: int, /) -> None:
[print('L'*i) for i in range (1,n+1)]
# 完成以下函数, 将 item 追加到 lst 列表的最后面, 并返回 None, 比如
#
# my_lst = [3, 1, 2]
# res = f004(my_lst, 0)
# res is None
# my_lst == [3, 1, 2, 0]
#
# my_lst = ["d", "f", "b"]
# res = f004(my_lst, "a")
# res is None
# my_lst == ["d", "f", "b", "a"]
#
def f004(lst: List, item: Any) -> None:
lst.append(item)
# 完成以下函数, 不修改 lst 列表, 但返回一个新的列表实例,
# 由原 lst 列表追加 item 构成, 比如
#
# my_lst = [3, 1, 2]
# res = f005(my_lst, 0)
# res is not my_lst
# res == [3, 1, 2, 0]
#
# my_lst = ["d", "f", "b"]
# res = f005(my_lst, "a")
# res is not my_lst
# res == ["d", "f", "b", "a"]
#
def f005(lst: List, item: Any) -> List:
return lst + [item]
# 完成以下函数, 将任意数量的位置实参追加到 lst 列表的最后面,
# 并返回 None, 比如
#
# my_lst = [3, 1, 2]
# res = f006(my_lst, 5, 0)
# res is None
# my_lst == [3, 1, 2, 5, 0]
#
# my_lst = ["d", "f", "b"]
# res = f006(my_lst, "a", "h", "c")
# res is None
# my_lst == ["d", "f", "b", "a", "h", "c"]
#
def f006(lst: List, *args) -> None:
lst.extend(args)
# 完成以下函数, 不修改 lst 列表, 但返回一个新的列表实例,
# 由原 lst 列表追加任意数量的位置实参构成, 比如
#
# my_lst = [3, 1, 2]
# res = f007(my_lst, 5, 0)
# res is not my_lst
# res == [3, 1, 2, 5, 0]
#
# my_lst = ["d", "f", "b"]
# res = f007(my_lst, "a", "h", "c")
# res is not my_lst
# res == ["d", "f", "b", "a", "h", "c"]
#
def f007(lst: List, *args) -> List:
return lst + list(args)
# 完成以下函数, 返回一个基于 lst 的新列表实例,
# 不包含 lst 从左起的 n 个元素, 比如
#
# my_lst = [3, 1, 2, 5, 6]
# res = f008(my_lst, 2)
# res is not my_lst
# res == [2, 5, 6]
#
# my_lst = ["d", "f", "b", "a", "h", "c"]
# res = f008(my_lst, 4)
# res is not my_lst
# res == ["h", "c"]
#
def f008(lst: List, n: int) -> List:
return lst[n:]
# 完成以下函数, 返回一个基于 lst 的新列表实例,
# 由 lst 从左起的 n 个元素和 lst 从右起的 n 个元素构成, 比如
#
# my_lst = [3, 1, 2, 5, 6]
# res = f009(my_lst, 2)
# res is not my_lst
# res == [3, 1, 5, 6]
#
# my_lst = ["d", "f", "b", "a", "h", "c"]
# res = f009(my_lst, 1)
# res is not my_lst
# res == ["d", "c"]
#
def f009(lst: List, n: int) -> List:
return lst[:n] + lst[-n:]
# 完成以下函数, 删除 lst 末尾的 n 个元素, 并返回 None, 比如
#
# my_lst = [3, 1, 2, 5, 6]
# res = f010(my_lst, 2)
# res is None
# my_lst == [3, 1, 2]
#
# my_lst = ["d", "f", "b", "a", "h", "c"]
# res = f010(my_lst, 3)
# res is None
# my_lst == ["d", "f", "b"]
#
def f010(lst: List, n: int) -> None:
del lst[-n:]
# 完成以下函数, 基于 lst 生成一个新的列表实例, 其元素基于 lst,
# 但其中的 None 被全部移动到最右边尾部, 比如
#
# my_lst = [3, None, 2, 5, 6]
# res = f011(my_lst)
# res is not my_lst
# res == [3, 2, 5, 6, None]
#
# my_lst = [None, "f", "b", "a", None, "c"]
# res = f011(my_lst)
# res is not my_lst
# res == ["f", "b", "a", "c", None, None]
#
def f011(lst: List) -> List:
return [x for x in lst if x is not None] + [x for x in lst if x is None]
# 完成以下函数, 修改 lst 列表, 令其 None 元素全部移动到最右边尾部, 比如
#
# my_lst = [3, None, 2, 5, 6]
# res = f012(my_lst)
# res is None
# my_lst == [3, 2, 5, 6, None]
#
# my_lst = [None, "f", "b", "a", None, "c"]
# res = f012(my_lst)
# res is None
# my_lst == ["f", "b", "a", "c", None, None]
#
# 提示: 可以使用 for 语句, if 语句, del 语句, 和 enumerate 内置函数
def f012(lst: List) -> None:
none_count = lst.count(None)
for _ in range(none_count):
lst.remove(None)
lst.append(None)
# 完成以下函数, 返回一个新的列表实例, 内容是 hosts 和 ports 的元素
# 用 : 连接得出的新字符串, 比如
#
# my_hosts = ["www.cueb.edu.cn", "www.baidu.com", "www.google.com"]
# my_ports = [80, 443, 22]
# res = f013(my_hosts, my_ports)
# res == ["www.cueb.edu.cn:80", "www.baidu.com:443", "www.google.com:22"]
#
# 提示: 可以使用 zip 内置函数和 f-string
def f013(hosts: List[str], ports: List[int]) -> List[str]:
return [f"{host}:{port}" for host, port in zip(hosts, ports)]
# 完成以下函数, 使 lst 列表的元素反转, 并返回 None, 比如
#
# my_lst = [3, 1, 2]
# res = f014(my_lst)
# res is None
# my_lst == [2, 1, 3]
#
# my_lst = ["d", "f", "b"]
# res = f014(my_lst)
# res is None
# my_lst == ["b", "f", "d"]
#
def f014(lst: List[Any]) -> None:
lst.reverse()
# 完成以下函数, 基于 lst 列表返回一个新的列表实例, 其内容是 lst 内容的反转,
# 而 lst 的内容要保持不变, 比如
#
# my_lst = [3, 1, 2]
# res = f015(my_lst)
# res == [2, 1, 3]
#
# my_lst = ["d", "f", "b"]
# res = f015(my_lst)
# res == ["b", "f", "d"]
#
def f015(lst: List[Any]) -> List[Any]:
return lst[::-1]
# 完成以下函数, 使 lst 列表的元素变为升序排序, 并返回 None, 比如
#
# my_lst = [3, 1, 2]
# res = f016(my_lst)
# res is None
# my_lst == [1, 2, 3]
#
# my_lst = ["d", "f", "b"]
# res = f016(my_lst)
# res is None
# my_lst == ["b", "d", "f"]
#
def f016(lst: List[Any]) -> None:
lst.sort()
# 完成以下函数, 基于 lst 列表返回一个新的列表实例,
# 其内容是 lst 经过升序排序后的内容, 而 lst 的内容要保持不变, 比如
#
# my_lst = [3, 1, 2]
# res = f017(my_lst)
# res == [1, 2, 3]
#
# my_lst = ["d", "f", "b"]
# res = f017(my_lst)
# res == ["b", "d", "f"]
#
def f017(lst: List[Any]) -> List[Any]:
return sorted(lst)
# 完成以下函数, 基于 lst 列表返回一个新的列表实例,
# 其内容是 lst 经过排序后的内容, 其排序的标准是按元素的绝对值进行升序排序,
# 而 lst 的内容要保持不变, 比如
#
# my_lst = [3, 1, -2]
# res = f018(my_lst)
# res == [1, -2, 3]
#
# my_lst = [4, 1, -3, 2]
# res = f018(my_lst)
# res == [1, 2, -3, 4]
#
# 提示: 可以使用 abs 内置函数
def f018(lst: List[int]) -> List[int]:
return sorted(lst,key=abs)
# 完成以下函数, 基于 lst 列表返回一个新的列表实例,
# lst 列表里的内容是整数二元组,
# 排序的标准是按二元组前后两元素的差值进行升序排序, 比如
#
# my_lst = [(4, 3), (5, 1), (3, 1)]
# res = f019(my_lst)
# res == [(4, 3), (3, 1), (5, 1)]
#
# my_lst = [(4, -3), (5, 5), (1, 2), (3, 1)]
# res = f019(my_lst)
# res == [(1, 2), (5, 5), (3, 1), (4, -3)]
#
# 提示: 可以使用 lambda 匿名函数
def f019(lst: List[Tuple[int, int]]) -> List[Tuple[int, int]]:
return sorted(lst, key=lambda x: x[0] - x[1])
# 完成以下函数, 基于 lst 列表返回一个排序后的新的列表实例,
# order 列表是人为给定的排序顺序, 比如 order 可以是
#
# ["优秀", "良好", "及格", "不及格"]
#
# 或者可以是
#
# ["Mon", "Tue", "Wen", "Thu", "Fri", "Sat", "Sun"]
#
# 排序的标准是按 order 所指定的元素顺序进行升序排序, 比如
#
# my_lst = ["良好", "不及格", "优秀", "良好"]
# my_order = ["优秀", "良好", "及格", "不及格"]
# res = f020(my_lst)
# res == ["优秀", "良好", "良好", "不及格"]
#
# my_lst = ["Tue", "Fri", "Tue", "Fri", "Mon"]
# my_order = ["Mon", "Tue", "Wen", "Thu", "Fri", "Sat", "Sun"]
# res = f020(my_lst)
# res == ["Mon", "Tue", "Tue", "Fri", "Fri"]
#
# 提示: 可以学习一下 str 类型的 index 方法
def f020(lst: List[Any], order: List[Any]) -> List[Any]:
return sorted(lst, key=lambda x: order.index(x))
# 完成以下函数, 实现列表列表 (list of list) 的转置 (transpose), 比如
#
# my_lst = [
# ["aaa", "bbb", "ccc"],
# [3.112, 4.721, 1.874],
# ]
# f021(my_lst) == [
# ["aaa", 3.112],
# ["bbb", 4.721],
# ["ccc", 1.874],
# ]
#
# my_lst = [
# ["ddd", "eee"],
# [8.764, 4.329],
# [0.926, 3.927],
# [5.551, 7.452],
# ]
# f021(my_lst) == [
# ["ddd", 8.764, 0.926, 5.551],
# ["eee", 4.329, 3.927, 7.452],
# ]
#
# 提示: 可以使用列表推导式, 解包表达式和 zip 内置函数
def f021(lst: List[List]) -> List[List]:
return [list(x) for x in zip(*lst)]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。