zxy0307 commited on
Commit
338c929
·
verified ·
1 Parent(s): 56e99af

Upload 5 files

Browse files
01_装饰器.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
02_生成器和迭代器.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
03_正则表达式.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
04_Python新特性.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
05_内存管理.ipynb ADDED
@@ -0,0 +1,896 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "0e1b9f4a",
6
+ "metadata": {},
7
+ "source": [
8
+ "## 元类\n",
9
+ "- 什么是元类?\n",
10
+ " - 动态创建类\n",
11
+ " - 元类 -> 类\n",
12
+ " - 类 -> 对象\n",
13
+ "\n",
14
+ "- 用途?\n",
15
+ " - 可以动态创建类\n",
16
+ "\n",
17
+ "- 如何使用?type()\n",
18
+ " 1. 查看目标对象的数据类型\n",
19
+ " 2. 可以使用type,动态创建类\n",
20
+ "\n",
21
+ "语法: 类 = type(类名, (父类...), {属性,方法})"
22
+ ]
23
+ },
24
+ {
25
+ "cell_type": "code",
26
+ "execution_count": 1,
27
+ "id": "60c39e20",
28
+ "metadata": {
29
+ "ExecuteTime": {
30
+ "end_time": "2023-10-13T13:37:23.492372Z",
31
+ "start_time": "2023-10-13T13:37:23.481653Z"
32
+ }
33
+ },
34
+ "outputs": [
35
+ {
36
+ "name": "stdout",
37
+ "output_type": "stream",
38
+ "text": [
39
+ "<__main__.Person object at 0x107537bb0>\n",
40
+ "3\n",
41
+ "狗狗趴着睡觉\n",
42
+ "Yellow\n",
43
+ "动物需要吃东西\n",
44
+ "Dog\n"
45
+ ]
46
+ }
47
+ ],
48
+ "source": [
49
+ "# 创建一个默认父类,不包含任何属性方法的类\n",
50
+ "Person = type('Person', (), {})\n",
51
+ "\n",
52
+ "# 可以用Person创建对象\n",
53
+ "p1 = Person()\n",
54
+ "print(p1)\n",
55
+ "\n",
56
+ "\n",
57
+ "class Animal():\n",
58
+ " def __init__(self, color):\n",
59
+ " self.color = color\n",
60
+ "\n",
61
+ " def eat(self):\n",
62
+ " print(\"动物需要吃东西\")\n",
63
+ "\n",
64
+ "\n",
65
+ "def sleep(self):\n",
66
+ " print(\"狗狗趴着睡觉\")\n",
67
+ "\n",
68
+ "\n",
69
+ "# 使用 type动态创建一个类,父类就是Animal\n",
70
+ "Dog = type('Dog', (Animal,), {'age': 3, 'sleep': sleep})\n",
71
+ "dog = Dog('Yellow')\n",
72
+ "print(dog.age)\n",
73
+ "dog.sleep()\n",
74
+ "\n",
75
+ "# 继承了父类中的特性\n",
76
+ "print(dog.color)\n",
77
+ "\n",
78
+ "# 继承了父类的方法\n",
79
+ "dog.eat()\n",
80
+ "\n",
81
+ "print(Dog.__name__)"
82
+ ]
83
+ },
84
+ {
85
+ "cell_type": "markdown",
86
+ "id": "f1b9fa23",
87
+ "metadata": {},
88
+ "source": [
89
+ "## 类装饰器"
90
+ ]
91
+ },
92
+ {
93
+ "cell_type": "markdown",
94
+ "id": "ef2feec8",
95
+ "metadata": {},
96
+ "source": [
97
+ "在不修改函数源代码的前提下,增加新的功能"
98
+ ]
99
+ },
100
+ {
101
+ "cell_type": "code",
102
+ "execution_count": 2,
103
+ "id": "073f02c2",
104
+ "metadata": {
105
+ "ExecuteTime": {
106
+ "end_time": "2023-10-13T13:40:58.644893Z",
107
+ "start_time": "2023-10-13T13:40:58.639390Z"
108
+ }
109
+ },
110
+ "outputs": [
111
+ {
112
+ "name": "stdout",
113
+ "output_type": "stream",
114
+ "text": [
115
+ "--初始化--\n",
116
+ "func name is test\n",
117
+ "--装饰器中的功能--\n",
118
+ "--test 函数---\n"
119
+ ]
120
+ }
121
+ ],
122
+ "source": [
123
+ "class Test(object):\n",
124
+ " def __init__(self, func):\n",
125
+ " print(\"--初始化--\")\n",
126
+ " print(\"func name is %s\" % func.__name__)\n",
127
+ " self.__func = func\n",
128
+ " \n",
129
+ " # 重写该方法后,对象可以直接进行调用\n",
130
+ " def __call__(self):\n",
131
+ " print(\"--装饰器中的功能--\")\n",
132
+ " self.__func()\n",
133
+ "\n",
134
+ " \n",
135
+ "\n",
136
+ "@Test # @Test 等价于 test = Test(test) 装饰器特性\n",
137
+ "def test():\n",
138
+ " print('--test 函数---')\n",
139
+ "\n",
140
+ "\n",
141
+ "# 本身test指向函数test,但是装饰器将test指向了对象。\n",
142
+ "# 对象本身不可以被调用,但是重写__call__方法之后则会被调用\n",
143
+ "test()"
144
+ ]
145
+ },
146
+ {
147
+ "cell_type": "markdown",
148
+ "id": "6477951f",
149
+ "metadata": {},
150
+ "source": [
151
+ "## 对象池\n",
152
+ "\n",
153
+ "对象池:\n",
154
+ "1. 数值类型\n",
155
+ " - 小整数池\n",
156
+ " - 小整数: [-5, 256]\n",
157
+ " - 程序开始时,一次性加载到内存\n",
158
+ " - LEGB(局部变量,闭包中的变量,全局变量,内建变量)\n",
159
+ " - 全局都是同一个\n",
160
+ "\n",
161
+ " - 大整数池\n",
162
+ " - 每创建一个不是小整数范围内的变量,都会被自动存储到大整数池中\n",
163
+ "\n",
164
+ "2. 字符串\n",
165
+ " - intern机制\n",
166
+ " - 每个单词(字符串),不夹杂空格或者其他符号,默认开启intern机制,共享内存,靠引用计数决定是否销毁"
167
+ ]
168
+ },
169
+ {
170
+ "cell_type": "code",
171
+ "execution_count": 3,
172
+ "id": "8ab4b9f2",
173
+ "metadata": {
174
+ "ExecuteTime": {
175
+ "end_time": "2023-10-13T13:47:23.366013Z",
176
+ "start_time": "2023-10-13T13:47:23.358918Z"
177
+ }
178
+ },
179
+ "outputs": [
180
+ {
181
+ "name": "stdout",
182
+ "output_type": "stream",
183
+ "text": [
184
+ "4367209936\n",
185
+ "4367209936\n"
186
+ ]
187
+ }
188
+ ],
189
+ "source": [
190
+ "# 发现删除a后,b的地址依旧是删除之前的那个地址(是否删除,小整数都常驻内存)\n",
191
+ "a = 100\n",
192
+ "print(id(a))\n",
193
+ "\n",
194
+ "del a\n",
195
+ "b = 100\n",
196
+ "print(id(b))"
197
+ ]
198
+ },
199
+ {
200
+ "cell_type": "markdown",
201
+ "id": "fb237a6e",
202
+ "metadata": {},
203
+ "source": [
204
+ "## 垃圾回收机制:GC机制\n",
205
+ "Python:\n",
206
+ "1. 引用计数机制为主\n",
207
+ " - 如何获取一个对象的引用计数?\n",
208
+ " - sys.getrefcount(a)\n",
209
+ " - 刚创建对象引用计数为2\n",
210
+ "\n",
211
+ " - 增加引用计数操作\n",
212
+ " 1. 如果有新的对象对象使用该对象,+1\n",
213
+ " 2. 装进列表+1\n",
214
+ " 3. 作为函数参数+1\n",
215
+ " \n",
216
+ " - 减少引用计数操作\n",
217
+ " 1. 如果有新的对象对象使用该对象,新对象不再使用-1\n",
218
+ " 2. 从列表中移除-1\n",
219
+ " 3. 函数调用结束\n",
220
+ " 4. del 显示销毁\n",
221
+ "\n",
222
+ "\n",
223
+ "2. 隔代回收为辅助\n",
224
+ " - 原理:随着时间的推进,程序冗余对象逐渐增多,达到一定数量(阈值),系统进行回收\n"
225
+ ]
226
+ },
227
+ {
228
+ "cell_type": "code",
229
+ "execution_count": 5,
230
+ "id": "44b61533",
231
+ "metadata": {
232
+ "ExecuteTime": {
233
+ "end_time": "2023-10-13T14:32:02.775704Z",
234
+ "start_time": "2023-10-13T14:32:02.764763Z"
235
+ }
236
+ },
237
+ "outputs": [
238
+ {
239
+ "name": "stdout",
240
+ "output_type": "stream",
241
+ "text": [
242
+ "开辟内存空间\n",
243
+ "创建对象at:0x10793b1f0\n",
244
+ "a的引用计数为:2\n",
245
+ "a的引用计数为:3\n",
246
+ "a的引用计数为:4\n",
247
+ "<__main__.AA object at 0x10793b1f0>\n",
248
+ "a的引用计数为:6\n",
249
+ "a的引用计数为:4\n",
250
+ "----------------------\n",
251
+ "a的引用计数为:3\n",
252
+ "a的引用计数为:2\n"
253
+ ]
254
+ }
255
+ ],
256
+ "source": [
257
+ "\"\"\"垃圾回收机制\"\"\"\n",
258
+ "import sys\n",
259
+ "\n",
260
+ "class AA():\n",
261
+ " def __new__(cls, *args, **kwargs):\n",
262
+ " \"\"\"创建对象开辟内存时调用\"\"\"\n",
263
+ " \n",
264
+ " print(\"开辟内存空间\")\n",
265
+ " return super(AA, cls).__new__(cls)\n",
266
+ " \n",
267
+ "\n",
268
+ " def __init__(self):\n",
269
+ " \"\"\"初始化方法\"\"\"\n",
270
+ " print(\"创建对象at:%s\" % hex(id(self)))\n",
271
+ "\n",
272
+ "\n",
273
+ " def __del__(self):\n",
274
+ " \"\"\"对象被系统回之前,会调用该方法\"\"\"\n",
275
+ " print(\"%s say bye bye\" % hex(id(self)))\n",
276
+ "\n",
277
+ "\n",
278
+ "def test1(aaa):\n",
279
+ " print(aaa)\n",
280
+ " print('a的引用计数为:%d' % sys.getrefcount(a))\n",
281
+ "\n",
282
+ "\n",
283
+ "a = AA()\n",
284
+ "print('a的引用计数为:%d' % sys.getrefcount(a))\n",
285
+ "\n",
286
+ "b = a\n",
287
+ "print('a的引用计数为:%d' % sys.getrefcount(a))\n",
288
+ "\n",
289
+ "list1 = [a]\n",
290
+ "print('a的引用计数为:%d' % sys.getrefcount(a))\n",
291
+ "\n",
292
+ "test1(a) # 作为函数参数时计数+1,创建形参时再+1 \n",
293
+ "\n",
294
+ "# 函数调用结束,计数-1,形参销毁再-1\n",
295
+ "print('a的引用计数为:%d' % sys.getrefcount(a))\n",
296
+ "print('----------------------')\n",
297
+ "\n",
298
+ "b = 100\n",
299
+ "print('a的引用计数为:%d' % sys.getrefcount(a))\n",
300
+ "\n",
301
+ "list1.remove(a)\n",
302
+ "print('a的引用计数为:%d' % sys.getrefcount(a))"
303
+ ]
304
+ },
305
+ {
306
+ "cell_type": "markdown",
307
+ "id": "bd8552e2",
308
+ "metadata": {},
309
+ "source": [
310
+ "**gc模块的使用:**\n",
311
+ "\n",
312
+ "```\n",
313
+ "# 获取当前自动执行垃圾回收的计数器,返回一个长度为3的列表\n",
314
+ "1、gc.get_count()\n",
315
+ "\n",
316
+ "# 获取gc模块中自动执行垃圾回收的频率\n",
317
+ "2、gc.get_threshold()\n",
318
+ "\n",
319
+ "# 设置自动执行垃圾回收的频率\n",
320
+ "3、gc.set_threshold(threshold0[,threshold1,threshold2])\n",
321
+ "\n",
322
+ "# python3默认开启gc机制,可以使用该方法手动关闭gc机制\n",
323
+ "4、gc.disable()\n",
324
+ "\n",
325
+ "# 手动调用垃圾回收机制回收垃圾\n",
326
+ "5、gc.collect()\n",
327
+ "```"
328
+ ]
329
+ },
330
+ {
331
+ "cell_type": "code",
332
+ "execution_count": null,
333
+ "id": "12f1ce57",
334
+ "metadata": {
335
+ "ExecuteTime": {
336
+ "start_time": "2023-10-13T14:42:20.154Z"
337
+ }
338
+ },
339
+ "outputs": [
340
+ {
341
+ "name": "stdout",
342
+ "output_type": "stream",
343
+ "text": [
344
+ "new\n",
345
+ "object:born at 0x1044a8520\n",
346
+ "new\n",
347
+ "object:born at 0x1044a8e20\n",
348
+ "(200, 10, 10)\n",
349
+ "(47, 10, 0)\n",
350
+ "new\n",
351
+ "object:born at 0x1040bdac0\n",
352
+ "new\n",
353
+ "object:born at 0x1044a8ee0\n",
354
+ "(200, 10, 10)\n",
355
+ "(43, 10, 0)\n",
356
+ "new\n",
357
+ "object:born at 0x1044a8880\n",
358
+ "new\n",
359
+ "object:born at 0x1044a8fd0\n",
360
+ "(200, 10, 10)\n",
361
+ "(45, 10, 0)\n"
362
+ ]
363
+ }
364
+ ],
365
+ "source": [
366
+ "\"\"\"隔代回收机制\"\"\"\n",
367
+ "\n",
368
+ "import gc\n",
369
+ "import time\n",
370
+ "\n",
371
+ "\n",
372
+ "class AA():\n",
373
+ " def __new__(cls, *args, **kwargs):\n",
374
+ " print(\"new\")\n",
375
+ " return super(AA, cls).__new__(cls)\n",
376
+ "\n",
377
+ " def __init__(self):\n",
378
+ " print(\"object:born at %s\" % hex(id(self)))\n",
379
+ "\n",
380
+ " def __del__(self):\n",
381
+ " print(\"%s 被系统回收\" % hex(id(self)))\n",
382
+ "\n",
383
+ "\n",
384
+ "def start():\n",
385
+ " while True:\n",
386
+ " # 创建对象\n",
387
+ " a = AA()\n",
388
+ " b = AA()\n",
389
+ " \n",
390
+ " # 相互引用 -- 导致对象无法通过 del删除\n",
391
+ " a.v = b\n",
392
+ " b.v = a\n",
393
+ " del a\n",
394
+ " del b\n",
395
+ " \n",
396
+ " # 获取gc模块中自动执行垃圾回收的频率\n",
397
+ " print(gc.get_threshold())\n",
398
+ " \n",
399
+ " # 获取当前自动执行垃圾回收的计数器\n",
400
+ " print(gc.get_count())\n",
401
+ " \n",
402
+ " time.sleep(3)\n",
403
+ "\n",
404
+ "# 设置自动执行垃圾回收的频率\n",
405
+ "# 三个链表, 零代链表上的对象(新创建的对象都加入到零代链表), 引用数都是一,\n",
406
+ "# 每增加一个指针, 引用加一, 随后python会检测列表中的互相引用的对象, 根据规则减掉其引用计数. \n",
407
+ "# GC算法对链表一的引用减一,引用为0的,清除,不为0的到链表二, 链表二也执行GC算法, 链表三一样. \n",
408
+ "gc.set_threshold(200, 10, 10)\n",
409
+ "start()"
410
+ ]
411
+ },
412
+ {
413
+ "cell_type": "markdown",
414
+ "id": "6f6bdb71",
415
+ "metadata": {},
416
+ "source": [
417
+ "## 内建函数"
418
+ ]
419
+ },
420
+ {
421
+ "cell_type": "markdown",
422
+ "id": "e7c5b670",
423
+ "metadata": {},
424
+ "source": [
425
+ "### range函数"
426
+ ]
427
+ },
428
+ {
429
+ "cell_type": "code",
430
+ "execution_count": 3,
431
+ "id": "c2639318",
432
+ "metadata": {
433
+ "ExecuteTime": {
434
+ "end_time": "2023-10-13T14:46:07.440244Z",
435
+ "start_time": "2023-10-13T14:46:07.431154Z"
436
+ }
437
+ },
438
+ "outputs": [
439
+ {
440
+ "name": "stdout",
441
+ "output_type": "stream",
442
+ "text": [
443
+ "1 2 3 4 5 6 7 8 9 10 \n",
444
+ "1 3 5 7 9 \n",
445
+ "10 9 8 7 6 5 4 3 2 1 0 \n",
446
+ "10 8 6 4 2 0 \n",
447
+ "True\n"
448
+ ]
449
+ }
450
+ ],
451
+ "source": [
452
+ "\"\"\"\n",
453
+ "range(stop)\n",
454
+ " 默认从0开始,直到stop-1\n",
455
+ " \n",
456
+ "range(start[,stop,step])\n",
457
+ " start:表示开始\n",
458
+ " \n",
459
+ " stop:表示结束(不包括该元素)\n",
460
+ " \n",
461
+ " step:表示迭代的步长\n",
462
+ " 默认为1\n",
463
+ " 负值,表示递减\n",
464
+ "\n",
465
+ "\"\"\"\n",
466
+ "\n",
467
+ "# 打印[1-10]之间的数据\n",
468
+ "from collections import Iterable\n",
469
+ "\n",
470
+ "for i in range(1, 11):\n",
471
+ " print(i, end=\" \")\n",
472
+ "print()\n",
473
+ "\n",
474
+ "for i in range(1, 11, 2):\n",
475
+ " print(i, end=\" \")\n",
476
+ "print()\n",
477
+ "\n",
478
+ "# 倒序输出\n",
479
+ "for i in range(10, -1, -1):\n",
480
+ " print(i, end=\" \")\n",
481
+ "print()\n",
482
+ " \n",
483
+ "for i in range(10, -1, -2):\n",
484
+ " print(i, end=\" \")\n",
485
+ "print()\n",
486
+ "\n",
487
+ "# 是可迭代的\n",
488
+ "print(isinstance(range(10), Iterable))\n"
489
+ ]
490
+ },
491
+ {
492
+ "cell_type": "markdown",
493
+ "id": "11322ca3",
494
+ "metadata": {},
495
+ "source": [
496
+ "### map函数"
497
+ ]
498
+ },
499
+ {
500
+ "cell_type": "code",
501
+ "execution_count": 5,
502
+ "id": "e34b9cc3",
503
+ "metadata": {
504
+ "ExecuteTime": {
505
+ "end_time": "2023-10-13T14:53:43.701975Z",
506
+ "start_time": "2023-10-13T14:53:43.695783Z"
507
+ }
508
+ },
509
+ "outputs": [
510
+ {
511
+ "name": "stdout",
512
+ "output_type": "stream",
513
+ "text": [
514
+ "True\n",
515
+ "2 4 6 \n",
516
+ "-------------------\n",
517
+ "5 7 9 \n"
518
+ ]
519
+ }
520
+ ],
521
+ "source": [
522
+ "\"\"\"\n",
523
+ "map() 会根据提供的函数对指定序列做映射。\n",
524
+ "第一个参数 function 以参数序列中的每一个元素调用 function 函数,\n",
525
+ "返回包含每次 function 函数返回值的新列表。\n",
526
+ "\n",
527
+ "语法:map(function, iterable, ...)\n",
528
+ "\n",
529
+ "参数说明:\n",
530
+ " function -- 函数\n",
531
+ " iterable -- 一个或多个序列\n",
532
+ "\"\"\"\n",
533
+ "from collections import Iterable, Iterator\n",
534
+ "\n",
535
+ "# 创建两个列表\n",
536
+ "list1 = [1, 2, 3]\n",
537
+ "list2 = [4, 5, 6, 7]\n",
538
+ "\n",
539
+ "# 生成一个迭代器:list1中的每一个元素,进行function映射后,返回一个迭代器\n",
540
+ "it1 = map(lambda x: 2*x, list1)\n",
541
+ "\n",
542
+ "# \n",
543
+ "print(isinstance(it1, Iterator))\n",
544
+ "\n",
545
+ "for i in it1:\n",
546
+ " print(i, end=' ')\n",
547
+ "print()\n",
548
+ "\n",
549
+ "print('-------------------')\n",
550
+ "\n",
551
+ "# list1, list2 中的对应元素,传入 function, 返回一个迭代器\n",
552
+ "it2 = map(lambda x, y: x+y, list1, list2)\n",
553
+ "\n",
554
+ "for i in it2:\n",
555
+ " print(i, end=' ')\n",
556
+ "print()\n"
557
+ ]
558
+ },
559
+ {
560
+ "cell_type": "markdown",
561
+ "id": "10edf5bb",
562
+ "metadata": {},
563
+ "source": [
564
+ "### filter���数"
565
+ ]
566
+ },
567
+ {
568
+ "cell_type": "code",
569
+ "execution_count": 6,
570
+ "id": "7f128c40",
571
+ "metadata": {
572
+ "ExecuteTime": {
573
+ "end_time": "2023-10-13T14:55:10.920858Z",
574
+ "start_time": "2023-10-13T14:55:10.916685Z"
575
+ }
576
+ },
577
+ "outputs": [
578
+ {
579
+ "name": "stdout",
580
+ "output_type": "stream",
581
+ "text": [
582
+ "10 25 "
583
+ ]
584
+ }
585
+ ],
586
+ "source": [
587
+ "\"\"\"\n",
588
+ "filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。\n",
589
+ "\n",
590
+ "语法:\n",
591
+ " filter(function, iterable)\n",
592
+ " \n",
593
+ "参数说明:\n",
594
+ " function -- 判断函数。\n",
595
+ " iterable -- 可迭代对象。\n",
596
+ " \n",
597
+ "返回:\n",
598
+ " 返回迭代器。\n",
599
+ "\"\"\"\n",
600
+ "# 创建一个列表\n",
601
+ "list1 = [1, 2, 3, 0, 10, 0, 25]\n",
602
+ "\n",
603
+ "# 保留list1中 大于等于5的元素\n",
604
+ "it1 = filter(lambda x: x >= 5, list1)\n",
605
+ "for i in it1:\n",
606
+ " print(i, end=' ')"
607
+ ]
608
+ },
609
+ {
610
+ "cell_type": "markdown",
611
+ "id": "32a622b9",
612
+ "metadata": {},
613
+ "source": [
614
+ "### reduce函数"
615
+ ]
616
+ },
617
+ {
618
+ "cell_type": "code",
619
+ "execution_count": 8,
620
+ "id": "f44ba7d7",
621
+ "metadata": {
622
+ "ExecuteTime": {
623
+ "end_time": "2023-10-13T14:57:43.461711Z",
624
+ "start_time": "2023-10-13T14:57:43.457485Z"
625
+ }
626
+ },
627
+ "outputs": [
628
+ {
629
+ "name": "stdout",
630
+ "output_type": "stream",
631
+ "text": [
632
+ "和为:128\n"
633
+ ]
634
+ }
635
+ ],
636
+ "source": [
637
+ "\"\"\"\n",
638
+ "reduce() \n",
639
+ "\n",
640
+ "函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:\n",
641
+ " 函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,\n",
642
+ " 得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。\n",
643
+ " \n",
644
+ "语法:\n",
645
+ " reduce(function, iterable[, initializer])\n",
646
+ " \n",
647
+ "参数说明:\n",
648
+ " function -- 函数,有两个参数\n",
649
+ " iterable -- 可迭代对象\n",
650
+ " initializer -- 可选,初始参数\n",
651
+ " \n",
652
+ "返回:\n",
653
+ " 返回函数计算结果\n",
654
+ "\n",
655
+ "\"\"\"\n",
656
+ "import random\n",
657
+ "from functools import reduce\n",
658
+ "\n",
659
+ "# 生成列表\n",
660
+ "list1 = [37, 6, 33, 40, 12]\n",
661
+ "\n",
662
+ "# 进行累加\n",
663
+ "result = reduce(lambda x, y: x+y, list1)\n",
664
+ "\n",
665
+ "print('和为:%g' % result)"
666
+ ]
667
+ },
668
+ {
669
+ "cell_type": "markdown",
670
+ "id": "69630627",
671
+ "metadata": {},
672
+ "source": [
673
+ "### sorted函数"
674
+ ]
675
+ },
676
+ {
677
+ "cell_type": "code",
678
+ "execution_count": 9,
679
+ "id": "c9d08ae7",
680
+ "metadata": {
681
+ "ExecuteTime": {
682
+ "end_time": "2023-10-13T15:02:15.289028Z",
683
+ "start_time": "2023-10-13T15:02:15.279021Z"
684
+ }
685
+ },
686
+ "outputs": [
687
+ {
688
+ "name": "stdout",
689
+ "output_type": "stream",
690
+ "text": [
691
+ "aaa 19\n",
692
+ "bbb 18\n",
693
+ "ccc 20\n"
694
+ ]
695
+ }
696
+ ],
697
+ "source": [
698
+ "\"\"\"\n",
699
+ "sorted() 函数对所有可迭代的对象进行排序操作。\n",
700
+ "\n",
701
+ "sort 与 sorted 区别:\n",
702
+ " sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。\n",
703
+ " list 的 sort 方法返回的是对已经存在的列表进行操作,\n",
704
+ " 而内建函数 sorted 方法返回的是一个新的 list,而不会修改原有列表\n",
705
+ " \n",
706
+ "语法:\n",
707
+ " sorted(iterable[, key[, reverse]])\n",
708
+ " \n",
709
+ "参数说明:\n",
710
+ " iterable -- 可迭代对象。\n",
711
+ " key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。\n",
712
+ " reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)\n",
713
+ " \n",
714
+ "返回:\n",
715
+ " 返回重新排序的列表。\n",
716
+ "\n",
717
+ "\"\"\"\n",
718
+ "\n",
719
+ "# 列表中自定义对象的排序\n",
720
+ "class Student():\n",
721
+ " def __init__(self, name, age):\n",
722
+ " self.name = name\n",
723
+ " self.age = age\n",
724
+ "\n",
725
+ "stu1 = Student('aaa', 19)\n",
726
+ "stu2 = Student('ccc', 20)\n",
727
+ "stu3 = Student('bbb', 18)\n",
728
+ "\n",
729
+ "list1 = [stu1, stu2, stu3]\n",
730
+ "\n",
731
+ "# 按名字的字母顺序排序\n",
732
+ "list2 = sorted(list1, key=lambda x: x.name)\n",
733
+ "\n",
734
+ "for stu in list2:\n",
735
+ " print(stu.name, stu.age)"
736
+ ]
737
+ },
738
+ {
739
+ "cell_type": "markdown",
740
+ "id": "eeb58cce",
741
+ "metadata": {},
742
+ "source": [
743
+ "## 偏函数\n",
744
+ "把一个函数的某些参数给固定住(也就是设置默认值),返回一个新的函数,调用这个新函数会更简单"
745
+ ]
746
+ },
747
+ {
748
+ "cell_type": "code",
749
+ "execution_count": 11,
750
+ "id": "fd067dde",
751
+ "metadata": {
752
+ "ExecuteTime": {
753
+ "end_time": "2023-10-13T15:04:34.681807Z",
754
+ "start_time": "2023-10-13T15:04:34.677030Z"
755
+ }
756
+ },
757
+ "outputs": [
758
+ {
759
+ "name": "stdout",
760
+ "output_type": "stream",
761
+ "text": [
762
+ "10\n",
763
+ "10\n",
764
+ "10\n"
765
+ ]
766
+ }
767
+ ],
768
+ "source": [
769
+ "\"\"\"\n",
770
+ "偏函数的使用:\n",
771
+ "partial\n",
772
+ "\"\"\"\n",
773
+ "# 导包\n",
774
+ "from functools import partial\n",
775
+ "str1 = '1010'\n",
776
+ "\n",
777
+ "# str1是二进制,将其变为十进制数\n",
778
+ "result = int(str1, 2)\n",
779
+ "print(result)\n",
780
+ "\n",
781
+ "# 定义普通函数\n",
782
+ "def int2(n, base=2):\n",
783
+ " return int(n, base)\n",
784
+ "\n",
785
+ "result = int2(str1)\n",
786
+ "print(result)\n",
787
+ "\n",
788
+ "# 使用偏函数完成类似的功能\n",
789
+ "int3 = partial(int, base=2)\n",
790
+ "print(int3(str1))"
791
+ ]
792
+ },
793
+ {
794
+ "cell_type": "markdown",
795
+ "id": "b10525e0",
796
+ "metadata": {},
797
+ "source": [
798
+ "## wraps函数"
799
+ ]
800
+ },
801
+ {
802
+ "cell_type": "code",
803
+ "execution_count": 12,
804
+ "id": "5bc305e8",
805
+ "metadata": {
806
+ "ExecuteTime": {
807
+ "end_time": "2023-10-13T15:06:49.329444Z",
808
+ "start_time": "2023-10-13T15:06:49.324640Z"
809
+ }
810
+ },
811
+ "outputs": [
812
+ {
813
+ "name": "stdout",
814
+ "output_type": "stream",
815
+ "text": [
816
+ "test\n",
817
+ "求x*x的值\n"
818
+ ]
819
+ }
820
+ ],
821
+ "source": [
822
+ "\"\"\"\n",
823
+ "wraps函数的作用:\n",
824
+ " functools.wraps 可以将原函数对象的指定属性\n",
825
+ " 复制给包装函数对象, 默认有 module、name、doc,或者通过参数选择\n",
826
+ "\"\"\"\n",
827
+ "from functools import wraps\n",
828
+ "\n",
829
+ "\n",
830
+ "def log(func):\n",
831
+ " @wraps(func)\n",
832
+ " def with_logging(*args, **kwargs):\n",
833
+ " print(\"%s was calling\" % func.__name__)\n",
834
+ " return func(*args, **kwargs)\n",
835
+ " return with_logging\n",
836
+ "\n",
837
+ "\n",
838
+ "@log # test = log(test)\n",
839
+ "def test(x):\n",
840
+ " \"\"\"求x*x的值\"\"\"\n",
841
+ " return x * x\n",
842
+ "\n",
843
+ "\n",
844
+ "print(test.__name__)\n",
845
+ "print(test.__doc__)"
846
+ ]
847
+ },
848
+ {
849
+ "cell_type": "code",
850
+ "execution_count": null,
851
+ "id": "f68a69bf",
852
+ "metadata": {},
853
+ "outputs": [],
854
+ "source": []
855
+ }
856
+ ],
857
+ "metadata": {
858
+ "kernelspec": {
859
+ "display_name": "Python 3 (ipykernel)",
860
+ "language": "python",
861
+ "name": "python3"
862
+ },
863
+ "language_info": {
864
+ "codemirror_mode": {
865
+ "name": "ipython",
866
+ "version": 3
867
+ },
868
+ "file_extension": ".py",
869
+ "mimetype": "text/x-python",
870
+ "name": "python",
871
+ "nbconvert_exporter": "python",
872
+ "pygments_lexer": "ipython3",
873
+ "version": "3.9.12"
874
+ },
875
+ "toc": {
876
+ "base_numbering": 1,
877
+ "nav_menu": {},
878
+ "number_sections": true,
879
+ "sideBar": true,
880
+ "skip_h1_title": false,
881
+ "title_cell": "Table of Contents",
882
+ "title_sidebar": "Contents",
883
+ "toc_cell": false,
884
+ "toc_position": {
885
+ "height": "calc(100% - 180px)",
886
+ "left": "10px",
887
+ "top": "150px",
888
+ "width": "265.390625px"
889
+ },
890
+ "toc_section_display": true,
891
+ "toc_window_display": true
892
+ }
893
+ },
894
+ "nbformat": 4,
895
+ "nbformat_minor": 5
896
+ }