Skip to content

Commit 68187bd

Browse files
committed
remove unnecessary files
1 parent 7067545 commit 68187bd

11 files changed

Lines changed: 41 additions & 183 deletions

File tree

.idea/.gitignore

Lines changed: 0 additions & 8 deletions
This file was deleted.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 0 additions & 96 deletions
This file was deleted.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

.idea/misc.xml

Lines changed: 0 additions & 4 deletions
This file was deleted.

.idea/modules.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.

.idea/python.iml

Lines changed: 0 additions & 12 deletions
This file was deleted.

.idea/vcs.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

README.md

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
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

2423
min_pairs = min(zip(prices.values(),prices.keys()))
2524
sorted_pairs = sorted(zip(prices.values(),prices.keys()))
@@ -34,13 +33,13 @@ sorted_keys = sorted(prices,prices.get)
3433
```python
3534
from 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
4645
import 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}]
353352
for x in flatten(items):
354353
print(x)
355354
```
356-
357355
## 9. 文件
358356

359357
1. 打印输出到文件中
@@ -383,27 +381,29 @@ def recv(maxsize, *, block):
383381
recv(1024, True) # TypeError
384382
recv(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)]
413413
res_t = [func() for func in t]
414414
res_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

usecase/5118/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
* 在根目录下(5118) 下打开终端,依次执行以下命令
1313

1414
```shell
15-
python3.9 -m venv venv
16-
source venv/bin/activate (Mac)
17-
.\venv\Scripts\activate.bat (Windows)
18-
pip install -r requirements.txt -i [https://pypi.doubanio.com/simple](https://pypi.doubanio.com/simple)
15+
python3.9 -m venv venv
16+
source venv/bin/activate (Mac)
17+
.\venv\Scripts\activate.bat (Windows)
18+
pip install -r requirements.txt -i https://pypi.doubanio.com/simple
1919
```
2020

2121
* **如果是跑大量数据,建议使用 kmeans 的方法,余弦相似度方法只是提供一个代码实现示例**
@@ -41,4 +41,4 @@ pip install -r requirements.txt -i [https://pypi.doubanio.com/simple](https://py
4141

4242
如果有问题,欢迎微信讨论
4343

44-
![](../../.gitbook/assets/Wechat.jpeg)
44+
<img src="/images/Wechat.jpeg" width = "220" align=center />

usecase/5118/kmeans.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from typing import List
77

88
import pandas as pd
9-
from common import aggregate_files, cut_word, get_path, get_stop_words, time_logger, write_excel
9+
from common import (aggregate_files, cut_word, get_path, get_stop_words,
10+
time_logger, write_excel)
1011
from logger import logger
1112
from sklearn.cluster import KMeans
1213
from sklearn.feature_extraction.text import CountVectorizer

0 commit comments

Comments
 (0)