摘要: 1, zip(可迭代对象1, 可迭代对象2...) from collections.abc import Iterator my_list_1 = [1, 2, 3, 4] my_list_2 = [6, 7, 8] result = zip(my_list_1, my_list_2) # 该迭代 阅读全文
posted @ 2023-09-04 23:37 yanghui01 阅读(4) 评论(0) 推荐(0) 编辑
摘要: 1, map(fn, 可迭代对象) 参数fn为一个参数的函数 lambda方式 my_list = [2, 3, 4, 5] result = map(lambda x: x * x, my_list) # 返回元素平方值的迭代器 print(type(result)) # <class 'map' 阅读全文
posted @ 2023-09-04 23:27 yanghui01 阅读(4) 评论(0) 推荐(0) 编辑
摘要: from collections.abc import Iterator def test_yield(n): for i in range(n): yield i * 2 # 暂停在当前步骤, 返回值, 下次继续从暂停位置继续 return -1 my_gen = test_yield(3) pr 阅读全文
posted @ 2023-09-04 23:25 yanghui01 阅读(6) 评论(0) 推荐(0) 编辑
摘要: Iterable: 表示可迭代对象, 用于获取迭代器(实现__iter__函数来返回迭代器) Iterator: 表示迭代器,用于遍历元素(通过__next__函数),迭代器也是可迭代对象 from collections.abc import Iterable, Iterator my_list 阅读全文
posted @ 2023-09-04 23:18 yanghui01 阅读(12) 评论(0) 推荐(0) 编辑
摘要: 推导式是什么? 用于创建容器对象的一种语法,主要用于创建list, dict, set, tuple。 1, list推导式 遍历+条件+产生的元素,用[]包装产生的每一个元素,其中条件是可选的。 my_list = [1, 2, 3] result = [elem + 1 for elem in 阅读全文
posted @ 2023-09-04 23:14 yanghui01 阅读(7) 评论(0) 推荐(0) 编辑
摘要: 三角函数 rad_30 = math.radians(30) rad_45 = math.radians(45) rad_60 = math.radians(60) rad_90 = math.radians(90) print(math.sin(rad_30)) # 0.5 print(math. 阅读全文
posted @ 2023-09-04 23:08 yanghui01 阅读(11) 评论(0) 推荐(0) 编辑
摘要: int, float print(int(1.2)) # 1 print(int("1")) # 1 print(int("0b11", 2)) # 3 print(int("27", 8)) # 23 print(int("0xF", 16)) # 15 print(int("0x1F", 16) 阅读全文
posted @ 2023-09-04 23:05 yanghui01 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 例1 fn = lambda x, y: x + y print(type(fn)) # <class 'function'> print(fn(1, 2)) # 3 例2 def add(x, y): return x + y print(type(add)) # <class 'function 阅读全文
posted @ 2023-09-04 23:03 yanghui01 阅读(3) 评论(0) 推荐(0) 编辑