Skip to content

Commit 360e122

Browse files
committed
#194-196
1 parent 172b733 commit 360e122

4 files changed

Lines changed: 82 additions & 6 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,9 @@
273273
| 191 | [格式化Pandas的时间列](md/191.md) | pandas apply | v1.0 | ⭐️⭐⭐⭐ |
274274
| 192 | [创建SQLite连接](md/192.md) | SQLite | v1.0 | ⭐️⭐⭐⭐ |
275275
| 193 | [json对象转python对象](md/193.md) | python json | v1.0 | ⭐️⭐⭐⭐ |
276-
277-
276+
| 194 | [python对象转json对象](md/194.md) | python json | v1.0 | ⭐️⭐⭐⭐ |
277+
| 195 | [发现列表前3个最大或最小数](md/195.md) | list heapq | v1.0 | ⭐️⭐⭐⭐ |
278+
| 196 | [使用堆排序列表为升序](md/196.md) | sort heapq | v1.0 | ⭐️⭐⭐⭐ |
278279
### Python 实战
279280

280281

md/194.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,45 @@
66
@version
77
@date 2020/04/01
88
```
9-
9+
#### 194 python对象转json对象
10+
11+
```python
12+
import json
13+
# a Python object (dict):
14+
python_obj = {
15+
"name": "David",
16+
"class":"I",
17+
"age": 6
18+
}
19+
print(type(python_obj))
20+
```
21+
22+
使用`json.dumps`方法转化为json对象:
23+
```
24+
# convert into JSON:
25+
j_data = json.dumps(python_obj)
26+
27+
# result is a JSON string:
28+
print(j_data)
29+
```
30+
31+
##### 带格式转为json
32+
33+
若字典转化为json对象后,保证键有序,且缩进4格,如何做到?
34+
35+
```python
36+
json.dumps(j_str, sort_keys=True, indent=4)
37+
```
38+
39+
例子:
40+
41+
```python
42+
import json
43+
j_str = {'4': 5, '6': 7, '1': 3, '2': 4}
44+
print(json.dumps(j_str, sort_keys=True, indent=4))
45+
```
46+
47+
48+
49+
<center>[上一个例子](193.md) [下一个例子](195.md)</center>
1050

11-
<center>[上一个例子](193.md) [下一个例子](195.md)</center>

md/195.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@
66
@version
77
@date 2020/04/02
88
```
9-
9+
#### 195 发现列表前3个最大或最小数
10+
11+
使用堆模块 heapq 里的 nlargest 方法:
12+
13+
```python
14+
import heapq as hq
15+
nums_list = [25, 35, 22, 85, 14, 65, 75, 22, 58]
16+
17+
# Find three largest values
18+
largest_nums = hq.nlargest(3, nums_list)
19+
print(largest_nums)
20+
```
21+
22+
相应的求最小3个数,使用堆模块 heapq 里的 nsmallest 方法:
23+
24+
```python
25+
import heapq as hq
26+
nums_list = [25, 35, 22, 85, 14, 65, 75, 22, 58]
27+
smallest_nums = hq.nsmallest(3, nums_list)
28+
print("\nThree smallest numbers are:", smallest_nums)
29+
```
30+
31+
32+
1033

1134
<center>[上一个例子](194.md) [下一个例子](196.md)</center>

md/196.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@
66
@version
77
@date 2020/04/03
88
```
9-
9+
10+
### 196 使用堆排序列表为升序
11+
12+
使用 heapq 模块,首先对列表建堆,默认建立小根堆,调用len(nums) 次heapop:
13+
14+
```python
15+
import heapq as hq
16+
17+
nums_list = [18, 14, 10, 9, 8, 7, 9, 3, 2, 4, 1]
18+
hq.heapify(nums_list)
19+
s_result = [hq.heappop(nums_list) for _ in range(len(nums_list))]
20+
print(s_result)
21+
```
22+
1023

1124
<center>[上一个例子](195.md) [下一个例子](197.md)</center>

0 commit comments

Comments
 (0)