Skip to content

Commit 104f7f7

Browse files
committed
2 parents 22cffc5 + 3b6274b commit 104f7f7

8 files changed

+234
-15
lines changed

README.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,47 @@
66

77
### 今日更新
88

9-
**2019年12月19日** 计算任意维坐标中两点之间距离
9+
**2019年12月20日** 让实例也能被调用
1010

11+
Python自定义一个类Student,如下:
1112
```python
12-
import math
13-
def distance(p0,p1,digits=2):
14-
a=map(lambda p: (p[0]-p[1])**2, zip(p0, p1))
15-
return round(math.sqrt(sum(a)),digits)
13+
class Student():
14+
...: def __init__(self,id,name):
15+
...: self.id = id
16+
...: self.name = name
17+
...: def __repr__(self):
18+
...: return 'id = '+self.id +', name = '+self.name
19+
1620
```
21+
创建实例:`xiaoming`:
1722

1823
```python
19-
distance((1,1),(2,2),digits=5) # 1.41421
20-
distance((1,2,3,4),(4,3,2,1)) # 4.47
24+
xiaoming = Student('001','xiaoming')
25+
xiaoming() # TypeError: 'Student' object is not callable
26+
```
27+
此时调用实例`xiaomng()`会抛出TypeError实例不能被调用的异常。
28+
29+
重写`__call__ `方法,实现`xiaomng()`可被调用:
30+
```python
31+
class Student():
32+
...: def __init__(self,id,name):
33+
...: self.id = id
34+
...: self.name = name
35+
...: def __repr__(self):
36+
...: return 'id = '+self.id +', name = '+self.name
37+
...: def __call__(self):
38+
...: print('Now, I can be called')
39+
...: print(f'my name is {self.name}')
40+
2141
```
42+
再次调用:
43+
```python
44+
In[1]: xiaoming = Student('001','xiaoming')
2245

46+
In[2]: xiaoming()
47+
OUT[2]: Now, I can be called
48+
my name is xiaoming
49+
```
2350

2451

2552
### 一、Python之基

data/cut_words.csv

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
��,12
2+
��,11
3+
Python,10
4+
��,7
5+
��,5
6+
����,4
7+
���,4
8+
��׼��,3
9+
ͨ��,3
10+
��,3
11+
�ṩ,3
12+
�,3
13+
��,3
14+
ϵͳ,3
15+
����,2
16+
�ο�,2
17+
����,2
18+
��,2
19+
��,2
20+
��,2
21+
��,2
22+
��,2
23+
ѡ,2
24+
��,2
25+
��д,2
26+
����,2
27+
����,2
28+
ƽ̨,2
29+
����,2
30+
����,2
31+
����,1
32+
�﷨,1
33+
����,1
34+
��,1
35+
��,1
36+
��,1
37+
����,1
38+
��,1
39+
һͬ,1
40+
����,1
41+
��,1
42+
��,1
43+
���а�,1
44+
һЩ,1
45+
�dz�,1
46+
�Ӵ�,1
47+
�漰,1
48+
��Χ,1
49+
ʮ��,1
50+
�㷺,1
51+
����,1
52+
����,1
53+
����,1
54+
Ŀ¼,1
55+
��ʾ,1
56+
���,1
57+
��,1
58+
��,1
59+
����,1
60+
(,1
61+
C,1
62+
),1
63+
����Ա,1
64+
����,1
65+
����,1
66+
����,1
67+
ʵ��,1
68+
��,1
69+
����,1
70+
�ļ�,1
71+
I/O,1
72+
����,1
73+
����,1
74+
����,1
75+
�ճ�,1
76+
���,1
77+
����,1
78+
��׼,1
79+
���,1
80+
����,1
81+
����,1
82+
��Щ,1
83+
����,1
84+
ר��,1
85+
���,1
86+
ͨ��,1
87+
��,1
88+
�ض�,1
89+
����,1
90+
Ϊ,1
91+
����,1
92+
API,1
93+
����,1
94+
��ǿ,1
95+
����ֲ��,1
96+
Windows,1
97+
�汾,1
98+
��װ,1
99+
����,1
100+
����,1
101+
����,1
102+
����,1
103+
��,1
104+
Unix,1
105+
��,1
106+
�ֳ�,1
107+
һ,1
108+
ϵ��,1
109+
������,1
110+
���,1
111+
����,1
112+
��Ҫ,1
113+
ʹ��,1
114+
��,1
115+
����,1
116+
����,1
117+
��ȡ,1
118+
����,1
119+
��,1
120+
ȫ��,1

md/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,13 @@
6161
#### 六、文件操作
6262
1. [批量修改后缀名](./批量修改后缀名.md)
6363
2. [返回两个文件的不同行的编号](返回两个文件的不同行的编号.md)
64-
3. [查找指定文件格式文件](查找指定文件格式文件.md)
64+
3. [查找指定后缀名的文件](查找指定文件格式文件.md)
6565
4. [使用Python实现群发邮件功能](自动群发邮件.md)
6666
5. [xls批量转化为xlsx](xls批量转化为xlsx.md)
6767
6. [获取指定文件路径下文件修改时间](获取指定文件路径下文件修改时间.md)
68-
7. [计算指定日期当月最后一天的日期和该月天数](计算指定日期当月最后一天的日期和该月天数.md)
69-
8. [文件批量压缩](文件批量压缩.md)
70-
9. [文件读写操作](文件读写操作.md)
71-
10. [字符串32位加密](字符串32位加密.md)
68+
7. [文件批量压缩](文件批量压缩.md)
69+
8. [文件读写操作](文件读写操作.md)
70+
9. [字符串32位加密](字符串32位加密.md)
7271

7372

7473
#### 七、字符串、正则和爬虫
@@ -81,7 +80,7 @@
8180
7. [格式化数字使用大全](str.format() 格式化数字的多种方法.md)
8281
8. [字符串32位加密](字符串32位加密.md)
8382
9. [反转字符串](反转字符串1.md)
84-
83+
10. [分词并保存结果](分词并保存结果.md)
8584

8685
#### 八、绘图
8786

md/分词并保存结果.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
**分词并保存结果**
2+
3+
`pkuseg`是北大开源的一个中文分词工具包。它在多个分词数据集上都有非常高的分词准确率,比经常使用的`jieba`分词性能和效果要更好。
4+
5+
下面使用`pkuseg``cut`函数,分词后统计前10频率词,并按照所有词的频次由高到低写入到文件`cut_words.csv` 中。
6+
7+
这是需要切分的段落:
8+
```python
9+
mystr = """Python 语言参考 描述了 Python 语言的具体语法和语义,
10+
这份库参考则介绍了与 Python 一同发行的标准库。
11+
它还描述了通常包含在 Python 发行版中的一些可选组件。
12+
Python 标准库非常庞大,所提供的组件涉及范围十分广泛,
13+
正如以下内容目录所显示的。这个库包含了多个内置模块 (以 C 编写),
14+
Python 程序员必须依靠它们来实现系统级功能,
15+
例如文件 I/O,此外还有大量以 Python 编写的模块,
16+
提供了日常编程中许多问题的标准解决方案。
17+
其中有些模块经过专门设计,
18+
通过将特定平台功能抽象化为平台中立的 API 来鼓励和加强 Python 程序的可移植性。
19+
Windows 版本的 Python 安装程序通常包含整个标准库,
20+
往往还包含许多额外组件。对于类 Unix 操作系统,
21+
Python 通常会分成一系列的软件包,
22+
因此可能需要使用操作系统所提供的包管理工具来获取部分或全部可选组件。"""
23+
```
24+
25+
几行代码就完成上述工作:
26+
27+
```python
28+
from pkuseg import pkuseg
29+
from collections import Counter
30+
31+
seg = pkuseg()
32+
words = seg.cut(mystr)
33+
frequency_sort = Counter(words).most_common()
34+
with open('./data/cut_words.csv', 'w') as f:
35+
for line in frequency_sort:
36+
f.write(str(line[0])+',' + str(line[1])+"\n")
37+
38+
print('writing done')
39+
```
40+
41+
出现最高频的前10个词语:
42+
```python
43+
Counter(words).most_common(10)
44+
# [('的', 12), (',', 11), ('Python', 10), ('。', 7), ('了', 5), ('包含', 4), ('组件', 4), ('标准库', 3), ('通常', 3), ('所', 3)]
45+
```

md/文件读写操作.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ def openfile(filename):
1818

1919

2020
# 写入文件信息
21-
2221
# example1
2322
# w写入,如果文件存在,则清空内容后写入,不存在则创建
2423
f = open(r"./data/test.txt", "w", encoding="utf-8")
2524
print(f.write("测试文件写入"))
2625
f.close
26+
2727
# example2
2828
# a写入,文件存在,则在文件内容后追加写入,不存在则创建
2929
f = open(r"./data/test.txt", "a", encoding="utf-8")

md/获取指定文件路径下文件修改时间.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
```python
2-
#递归获取目录下文件的修改时间
2+
#获取目录下文件的修改时间
33
import os
44
import datetime
55
print(f"当前时间:{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")

src/top_big_files.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from collections import defaultdict
2+
d = defaultdict(list)
3+
for root, dirs, files in os.walk('E:\guozhen3\08LOL\JDLOL\fengbi\jdlol'):
4+
for file in files:
5+
abs_path_file = os.path.join(root, file)
6+
file_size = os.path.getsize(abs_path_file)
7+
d[abs_path_file].append(file_size)

src/use_pkuseg.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pkuseg import pkuseg
2+
from collections import Counter
3+
4+
mystr = """Python 语言参考 描述了 Python 语言的具体语法和语义,这份库参考则介绍了与 Python 一同发行的标准库。它还描述了通常包含在 Python 发行版中的一些可选组件。
5+
6+
Python 标准库非常庞大,所提供的组件涉及范围十分广泛,正如以下内容目录所显示的。这个库包含了多个内置模块 (以 C 编写),Python 程序员必须依靠它们来实现系统级功能,例如文件 I/O,此外还有大量以 Python 编写的模块,提供了日常编程中许多问题的标准解决方案。其中有些模块经过专门设计,通过将特定平台功能抽象化为平台中立的 API 来鼓励和加强 Python 程序的可移植性。
7+
8+
Windows 版本的 Python 安装程序通常包含整个标准库,往往还包含许多额外组件。对于类 Unix 操作系统,Python 通常会分成一系列的软件包,因此可能需要使用操作系统所提供的包管理工具来获取部分或全部可选组件。"""
9+
10+
seg = pkuseg()
11+
words = seg.cut(mystr)
12+
top10 = Counter(words).most_common(10)
13+
# [('的', 12), (',', 11), ('Python', 10), ('。', 7), ('了', 5), ('包含', 4), ('组件', 4), ('标准库', 3), ('通常', 3), ('所', 3)]
14+
print(top10)
15+
16+
frequency_sort = Counter(words).most_common()
17+
with open('./data/cut_words.csv', 'w') as f:
18+
for line in frequency_sort:
19+
f.write(str(line[0])+',' + str(line[1])+"\n")
20+
21+
print('writing done')

0 commit comments

Comments
 (0)