1- ## Python 使用技巧
1+ # Python 使用技巧
22
33> This repositorie contains ancillary function which code in python3. Use it freely.
44>
55> Crawl web function may conatain error\( s\) as the web elements changed.
66>
77> If you had any idea to improve my code or you find any mistake in my file, please contact me. \( Email me at [email protected] \) 88
9- ### 1.在列表,字典,集合中根据条件筛选数据
9+ ## 1.在列表,字典,集合中根据条件筛选数据
1010
11- 需求:
12- 1 . 过滤 list 中大于 0 的数:[ 14, 0, -6, 0, -10, -8, -1, 19, -10, -16\]
13- >
14- 2 . 筛选出字典中值大于 0 的项目:{0: 9, 1: -3, 2: -8, 3: 6, 4: -4, 5: -4, 6: -7, 7: -8, 8: 7, 9: -3}
15- >
16- 3 . 筛选出集合中能被 2 整除的数:{3, 4, 9, 12, 15, 17, 19, 20}
17- >
18- 4 . 找到字典中值最小的健值对:prices = {'ACME': 45.23,'AAPL': 612.78,'IBM': 205.55,'HPQ': 37.20,'FB': 10.75}
19- >
20- 5 . 对字典排序,首先按照值排序,值相同再按照健排序 6. 对字典的健按照值排序
11+ 需求: 1. 过滤 list 中大于 0 的数:\[ 14, 0, -6, 0, -10, -8, -1, 19, -10, -16\] > ; 2. 筛选出字典中值大于 0 的项目:{0: 9, 1: -3, 2: -8, 3: 6, 4: -4, 5: -4, 6: -7, 7: -8, 8: 7, 9: -3} > ; 3. 筛选出集合中能被 2 整除的数:{3, 4, 9, 12, 15, 17, 19, 20} > ; 4. 找到字典中值最小的健值对:prices = {'ACME': 45.23,'AAPL': 612.78,'IBM': 205.55,'HPQ': 37.20,'FB': 10.75} > ; 5. 对字典排序,首先按照值排序,值相同再按照健排序 6. 对字典的健按照值排序
2112
2213``` python
2314list_bigger = [x for x in list_nums if x > 0 ]
@@ -30,7 +21,7 @@ sorted_keys = sorted(prices,prices.get)
3021# zip 创建的对象只能访问一次
3122```
3223
33- ### 2.命名,统计,字典
24+ ## 2.命名,统计,字典
3425
3526需求:在表格中,每一行的信息固定,为了访问某个位置的值,不使用索引
3627
@@ -92,7 +83,7 @@ for x in sample("abcdefgh", randint(3, 6)):
9283 order[x] = randint(4 ,10 )
9384```
9485
95- ### 3.字符串
86+ ## 3.字符串
9687
9788需求:根据多个分隔符,拆分字符串
9889
@@ -118,7 +109,7 @@ text = "07/20/2019"
118109res = re.sub(r " ( \d {2} ) \/ ( \d {2} ) \/ ( \d {4} ) " , r " \3 -\1 -\2 " , text)
119110```
120111
121- ### 4.遍历
112+ ## 4.遍历
122113
123114需求:同时遍历可迭代对象
124115
@@ -137,7 +128,7 @@ for x in chain(nums1, nums2, nums3):
137128 print (x)
138129```
139130
140- ### 5.类
131+ ## 5.类
141132
142133需求:为了确保用户输入正确格式的数,强制用户使用函数进行访问;为了简介,通过 property 自动调用函数,实现「设置属性自动调用函数的效果」
143134
@@ -245,7 +236,7 @@ fun = methodcaller("get_value", 2, -1)
245236print (fun(mc))
246237```
247238
248- ### 6. 装饰器
239+ ## 6. 装饰器
249240
2502411 . 需求:为某一个函数增加功能,不影响原来的函数
251242
@@ -319,7 +310,7 @@ for i in range(20):
319310 test()
320311```
321312
322- ### 7. 优先队列
313+ ## 7. 优先队列
323314
324315需求:实现一个有优先级的队列
325316
@@ -339,7 +330,7 @@ class PriorityQueue(object):
339330 return heapq.heappop(self ._queue)[- 1 ] if self ._queue else None
340331```
341332
342- ### 8. 展开嵌套的 list
333+ ## 8. 展开嵌套的 list
343334
344335``` python
345336from collections.abc import Iterable
@@ -356,7 +347,8 @@ items = [1, 2, [3, 4, (5, 6), 7], 8, "temp", "core", {-1, -2, -4, -6}]
356347for x in flatten(items):
357348 print (x)
358349```
359- ### 9. 文件
350+
351+ ## 9. 文件
360352
3613531 . 打印输出到文件中
362354
@@ -375,7 +367,7 @@ with open('somefile', 'xt') as f:
375367 f.write(' Hello\n ' )
376368```
377369
378- ### 10. 使用强制关键字参数
370+ ## 10. 使用强制关键字参数
379371
380372``` python
381373def recv (maxsize , * , block ):
@@ -385,28 +377,26 @@ def recv(maxsize, *, block):
385377recv(1024 , True ) # TypeError
386378recv(1024 , block = True ) # Ok
387379```
388- ### 11. 比较两个字典是否相等
380+
381+ ## 11. 比较两个字典是否相等
389382
390383* 键个数相等,键名一一对应,键值一一对应相等
391- ``` py
392- import json
393- from datetime import datetime
394384
395- from bson.json_util import default
385+ \`\`\` py
396386
387+ import json
397388
398- def compare_2_dict (dict_1 , dict_2 ):
399- return (json.dumps(dict_1, default = default, sort_keys = dict .keys) ==
400- json.dumps(dict_2, default = default, sort_keys = dict .keys))
389+ from datetime import datetime
401390
391+ from bson.json\_ util import default
402392
403- dicta = {" a" : datetime.now(), " b" : 2 }
404- dictb = {" a" : datetime.now(), " b" : 2 }
393+ def compare\_ 2\_ dict\( dict\_ 1, dict\_ 2\) : return \( json.dumps\( dict\_ 1, default=default, sort\_ keys=dict.keys\) == json.dumps\( dict\_ 2, default=default, sort\_ keys=dict.keys\)\)
405394
406- print (compare_2_dict( dicta, dictb))
395+ dicta = {"a": datetime.now \(\) , "b": 2} dictb = {"a": datetime.now \(\) , "b": 2}
407396
408- ```
397+ print \( compare \_ 2 \_ dict \( dicta, dictb \)\)
409398
399+ ``` text
410400### 12. Lambda
411401* 在 for loop 中使用 lambda,如果有赋值,请小心
412402
@@ -417,13 +407,15 @@ m = [lambda i=i: i for i in range(4)]
417407res_t = [func() for func in t]
418408res_m = [func() for func in m]
419409```
420- * res_t: [ 3, 3, 3, 3]
421- * rest_m: [ 0, 1, 2, 3]
422410
423- ### 13. Iterator
411+ * res\_ t: \[ 3, 3, 3, 3\]
412+ * rest\_ m: \[ 0, 1, 2, 3\]
413+
414+ ## 13. Iterator
415+
424416* iterator 不走回头路,可以利用此特性来判断 list 是否存在于另一个 list 中
425417
426- ``` py
418+ ``` python
427419# a = [1,2,3]
428420# b = [1,3,2,4,5,6]
429421# check if all num in a is in b, ordered
@@ -433,3 +425,4 @@ def check(a, b):
433425 b = iter (b)
434426 return all (i in b for i in a)
435427```
428+
0 commit comments