Skip to content

Commit 49154ad

Browse files
author
guozhen3
committed
199-201文件
1 parent 38b48a9 commit 49154ad

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ Python小例子 https://github.com/jackzhenguo/python-small-examples
238238
| 37 | [一行代码让 pip 安装加速 100 倍](md/176.md) | pip install | v1.0 | ⭐️⭐⭐ |
239239

240240

241-
### 综合小案例
241+
### 案例
242242
| 小例子 | 链接 | 标签 | 版本 | 难度 |
243243
| ---- | ---------------------------------- | ---- | ---- | ---- |
244244
| 1 | [不用else和if实现计算器](md/60.md) | operator | V1.0 | ⭐️⭐️⭐️ |
@@ -268,9 +268,9 @@ Python小例子 https://github.com/jackzhenguo/python-small-examples
268268
| 25 | [寻找第n次出现位置](md/125.md) | enumerator | V1.0 | ⭐️⭐️⭐️ |
269269
| 26 | [找出所有重复元素](md/127.md) | calendar,datetime | V1.0 | ⭐️⭐️⭐️⭐️ |
270270
| 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 | ⭐️⭐️⭐️⭐️⭐️ |
274274

275275

276276

@@ -374,4 +374,4 @@ Python关于数字的20个操作
374374

375375
15 [更有用的数组操作](http://www.zglg.work/numpy/numpy-more-array-operations/)
376376

377-
16 [生成随机数](http://www.zglg.work/numpy/numpy-generate-random-number/)
377+
16 [生成随机数](http://www.zglg.work/numpy/numpy-generate-random-number/)

md/199.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11

22
```markdown
33
@author jackzhenguo
4-
@desc
4+
@desc 求两点球面距离
55
@tag
66
@version
77
@date 2020/04/06
88
```
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+
```
1028

1129
<center>[上一个例子](198.md) [下一个例子](200.md)</center>

md/200.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11

22
```markdown
33
@author jackzhenguo
4-
@desc
4+
@desc 获取文件编码
55
@tag
66
@version
77
@date 2020/04/07
88
```
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+
```
1028

1129
<center>[上一个例子](199.md) [下一个例子](201.md)</center>

md/201.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11

22
```markdown
33
@author jackzhenguo
4-
@desc
4+
@desc 格式化json串
55
@tag
66
@version
77
@date 2020/04/08
88
```
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+
```
923
1024

1125
<center>[上一个例子](200.md) [下一个例子](202.md)</center>

0 commit comments

Comments
 (0)