File tree 4 files changed +60
-10
lines changed 4 files changed +60
-10
lines changed Original file line number Diff line number Diff line change @@ -238,7 +238,7 @@ Python小例子 https://github.com/jackzhenguo/python-small-examples
238
238
| 37 | [ 一行代码让 pip 安装加速 100 倍] ( md/176.md ) | pip install | v1.0 | ⭐️⭐⭐ |
239
239
240
240
241
- ### 综合小案例
241
+ ### 案例
242
242
| 小例子 | 链接 | 标签 | 版本 | 难度 |
243
243
| ---- | ---------------------------------- | ---- | ---- | ---- |
244
244
| 1 | [ 不用else和if实现计算器] ( md/60.md ) | operator | V1.0 | ⭐️⭐️⭐️ |
@@ -268,9 +268,9 @@ Python小例子 https://github.com/jackzhenguo/python-small-examples
268
268
| 25 | [ 寻找第n次出现位置] ( md/125.md ) | enumerator | V1.0 | ⭐️⭐️⭐️ |
269
269
| 26 | [ 找出所有重复元素] ( md/127.md ) | calendar,datetime | V1.0 | ⭐️⭐️⭐️⭐️ |
270
270
| 27 | [ 联合统计次数] ( md/128.md ) | Counter | V1.0 | ⭐️⭐️⭐️⭐️⭐️ |
271
-
272
-
273
-
271
+ | 28 | [ 求两点球面距离 ] ( md/199.md ) | math asin | V1.0 | ⭐️⭐️⭐️⭐️⭐️ |
272
+ | 29 | [ 获取文件编码 ] ( md/200.md ) | chardet | V1.0 | ⭐️⭐️⭐️⭐️⭐️ |
273
+ | 30 | [ 格式化json串 ] ( md/201.md ) | json | V1.0 | ⭐️⭐️⭐️⭐️⭐️ |
274
274
275
275
276
276
@@ -374,4 +374,4 @@ Python关于数字的20个操作
374
374
375
375
15 [ 更有用的数组操作] ( http://www.zglg.work/numpy/numpy-more-array-operations/ )
376
376
377
- 16 [ 生成随机数] ( http://www.zglg.work/numpy/numpy-generate-random-number/ )
377
+ 16 [ 生成随机数] ( http://www.zglg.work/numpy/numpy-generate-random-number/ )
Original file line number Diff line number Diff line change 1
1
2
2
``` markdown
3
3
@author jackzhenguo
4
- @desc
4
+ @desc 求两点球面距离
5
5
@tag
6
6
@version
7
7
@date 2020/04/06
8
8
```
9
-
9
+
10
+ ``` python
11
+ EARTH_RADIUS = 6378.137
12
+
13
+ import math
14
+ # 角度弧度计算公式
15
+ def get_radian (degree ):
16
+ return degree * 3.1415926 / 180.0
17
+ # 根据经纬度计算两点之间的距离,得到的单位是 千米
18
+ def get_distance (lat1 ,lng1 ,lat2 ,lng2 ):
19
+ radLat1 = get_radian(lat1)
20
+ radLat2 = get_radian(lat2)
21
+ a = radLat1 - radLat2 # 两点纬度差
22
+ b = get_radian(lng1) - get_radian(lng2); # 两点的经度差
23
+ s = 2 * math.asin(math.sqrt(math.pow(math.sin(a / 2 ), 2 ) +
24
+ math.cos(radLat1) * math.cos(radLat2) * math.pow(math.sin(b / 2 ), 2 )));
25
+ s = s * EARTH_RADIUS
26
+ return s
27
+ ```
10
28
11
29
<center >[上一个例子](198.md) [下一个例子](200.md)</center >
Original file line number Diff line number Diff line change 1
1
2
2
``` markdown
3
3
@author jackzhenguo
4
- @desc
4
+ @desc 获取文件编码
5
5
@tag
6
6
@version
7
7
@date 2020/04/07
8
8
```
9
-
9
+
10
+ ``` python
11
+ import chardet
12
+ from chardet import UniversalDetector
13
+
14
+ def get_encoding (file ):
15
+ with open (file , " rb" ) as f:
16
+ cs = chardet.detect(f.read())
17
+ return cs[' encoding' ]
18
+
19
+ detector = UniversalDetector()
20
+ with open (file , " rb" ) as f:
21
+ for line in f.readlines():
22
+ detector.feed(line)
23
+ if detector.done:
24
+ break
25
+ detector.close()
26
+ return detector.result
27
+ ```
10
28
11
29
<center >[上一个例子](199.md) [下一个例子](201.md)</center >
Original file line number Diff line number Diff line change 1
1
2
2
``` markdown
3
3
@author jackzhenguo
4
- @desc
4
+ @desc 格式化json串
5
5
@tag
6
6
@version
7
7
@date 2020/04/08
8
8
```
9
+ ``` python
10
+ import json
11
+
12
+
13
+ def format_json (json_str : str ):
14
+ dic = json.loads(json_str)
15
+
16
+ js = json.dumps(dic,
17
+ sort_keys = True ,
18
+ ensure_ascii = False ,
19
+ indent = 4 ,
20
+ separators = (' , ' , ' : ' ))
21
+ return js
22
+ ```
9
23
10
24
11
25
<center >[上一个例子](200.md) [下一个例子](202.md)</center >
You can’t perform that action at this time.
0 commit comments