11# Python 使用技巧
22
3- > This repository contains ancillary function which code in Python3 . Use it freely.
3+ > This repository contains ancillary function which code in python3 . Use it freely.
44>
55> Crawl web function may contain error(s) as the web elements changed.
66>
7- > If you had any idea to improve my code or you find any mistake in my file, please contact me. \ ( Email me at super76rui@icloud.com \ )
7+ > If you had any idea to improve my code or you find any mistake in my file, please contact me. (Email me at super76rui@icloud.com )
88
99## 1.在列表,字典,集合中根据条件筛选数据
1010
1111需求:
12- 1 . 过滤 list 中大于 0 的数:\[ 14, 0, -6, 0, -10, -8, -1, 19, -10, -16\] ;
13- 2 . 筛选出字典中值大于 0 的项目:{0: 9, 1: -3, 2: -8, 3: 6, 4: -4, 5: -4, 6: -7, 7: -8, 8: 7, 9: -3};
14- 3 . 筛选出集合中能被 2 整除的数:{3, 4, 9, 12, 15, 17, 19, 20};
15- 4 . 找到字典中值最小的健值对:prices = {'ACME': 45.23,'AAPL': 612.78,'IBM': 205.55,'HPQ': 37.20,'FB': 10.75};
16- 5 . 对字典排序,首先按照值排序,值相同再按照健排序;
17- 6 . 对字典的健按照值排序;
12+ 1 . 过滤 list 中大于 0 的数:[ 14, 0, -6, 0, -10, -8, -1, 19, -10, -16]
13+ 2 . 筛选出字典中值大于 0 的项目:{0: 9, 1: -3, 2: -8, 3: 6, 4: -4, 5: -4, 6: -7, 7: -8, 8: 7, 9: -3}
14+ 3 . 筛选出集合中能被 2 整除的数:{3, 4, 9, 12, 15, 17, 19, 20}
15+ 4 . 找到字典中值最小的健值对:prices = {'ACME': 45.23,'AAPL': 612.78,'IBM': 205.55,'HPQ': 37.20,'FB': 10.75}
16+ 5 . 对字典排序,首先按照值排序,值相同再按照健排序 6. 对字典的健按照值排序
1817
19- ``` python
20- list_bigger = [x for x in list_nums if x > 0 ]
21- dict_bigger = {k: v for k, v in dict_nums .items() if v > 0 }
22- set_two = {x for x in set_nums if not x % 2 }
18+ ``` py
19+ list_bigger = [x for x in list_numbers if x > 0 ]
20+ dict_bigger = {k: v for k, v in dict_numbers .items() if v > 0 }
21+ set_two = {x for x in set_numbers if not x % 2 }
2322
2423min_pairs = min (zip (prices.values(),prices.keys()))
2524sorted_pairs = sorted (zip (prices.values(),prices.keys()))
@@ -34,13 +33,13 @@ sorted_keys = sorted(prices,prices.get)
3433``` python
3534from collections import namedtuple
3635
37- stuendt = namedtuple(" Student" , [" name" , " age" , " sex" ])
36+ student = namedtuple(" Student" , [" name" , " age" , " sex" ])
3837
39- s1 = stuendt (name = " 12" , age = 12 , sex = " female" )
40- s2 = stuendt (name = " 12" , age = 12 , sex = " male" )
38+ s1 = student (name = " 12" , age = 12 , sex = " female" )
39+ s2 = student (name = " 12" , age = 12 , sex = " male" )
4140```
4241
43- 需求: 统计 \ [ 5, 2, 2, 3, 1, 5, 1, 3, 2, 4\ ] 出现次数最高的 3 的元素,并找到它们的出现次数 统计一个段落中出现次数最高的前 3 个元素,并确定次数
42+ 需求: 统计 [ 5, 2, 2, 3, 1, 5, 1, 3, 2, 4] 出现次数最高的 3 的元素,并找到它们的出现次数 统计一个段落中出现次数最高的前 3 个元素,并确定次数
4443
4544``` python
4645import re
@@ -63,8 +62,8 @@ com = times.most_common(3)
6362需求:根据字典中值的大小,对字典排序
6463
6564``` python
66- nums = {0 : 9 , 1 : - 3 , 2 : - 8 , 3 : 6 , 4 : - 4 , 5 : - 4 , 6 : - 7 , 7 : - 8 , 8 : 7 , 9 : - 3 }
67- res = sorted (nums .items(), key = lambda x : x[1 ])
65+ numbers = {0 : 9 , 1 : - 3 , 2 : - 8 , 3 : 6 , 4 : - 4 , 5 : - 4 , 6 : - 7 , 7 : - 8 , 8 : 7 , 9 : - 3 }
66+ res = sorted (numbers .items(), key = lambda x : x[1 ])
6867```
6968
7069需求:找到多个字典中的公共键
@@ -353,7 +352,6 @@ items = [1, 2, [3, 4, (5, 6), 7], 8, "temp", "core", {-1, -2, -4, -6}]
353352for x in flatten(items):
354353 print (x)
355354```
356-
357355## 9. 文件
358356
3593571 . 打印输出到文件中
@@ -383,27 +381,29 @@ def recv(maxsize, *, block):
383381recv(1024 , True ) # TypeError
384382recv(1024 , block = True ) # Ok
385383```
386-
387384## 11. 比较两个字典是否相等
388385
389386* 键个数相等,键名一一对应,键值一一对应相等
387+ ``` py
388+ import json
389+ from datetime import datetime
390390
391- \`\`\` py
391+ from bson.json_util import default
392392
393- import json
394393
395- from datetime import datetime
394+ def compare_2_dict (dict_1 , dict_2 ):
395+ return (json.dumps(dict_1, default = default, sort_keys = dict .keys) ==
396+ json.dumps(dict_2, default = default, sort_keys = dict .keys))
396397
397- from bson.json\_ util import default
398398
399- 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\)\)
399+ dicta = {" a" : datetime.now(), " b" : 2 }
400+ dictb = {" a" : datetime.now(), " b" : 2 }
400401
401- dicta = {"a": datetime.now \(\) , "b": 2} dictb = {"a": datetime.now \(\) , "b": 2}
402+ print (compare_2_dict(dicta, dictb))
402403
403- print \( compare \_ 2 \_ dict \( dicta, dictb \)\)
404+ ```
404405
405- ``` text
406- ### 12. Lambda
406+ ## 12. Lambda
407407* 在 for loop 中使用 lambda,如果有赋值,请小心
408408
409409``` py
@@ -413,15 +413,13 @@ m = [lambda i=i: i for i in range(4)]
413413res_t = [func() for func in t]
414414res_m = [func() for func in m]
415415```
416-
417- * res\_ t: \[ 3, 3, 3, 3\]
418- * rest\_ m: \[ 0, 1, 2, 3\]
416+ * res_t: [ 3, 3, 3, 3]
417+ * rest_m: [ 0, 1, 2, 3]
419418
420419## 13. Iterator
421-
422420* iterator 不走回头路,可以利用此特性来判断 list 是否存在于另一个 list 中
423421
424- ``` python
422+ ``` py
425423# a = [1,2,3]
426424# b = [1,3,2,4,5,6]
427425# check if all num in a is in b, ordered
0 commit comments