Skip to content

Commit 57881a3

Browse files
committed
添加新内容
Former-commit-id: fbd6818 Former-commit-id: 97fa4db13d49d503c9895c46fc8a88a1d6a844e4 [formerly 5c8e072d99de830070c734800b8a5cfdb4345583] [formerly 16f59ebcfe637bc2cc8fe0d58f58548b821e9152 [formerly c5e0cb069751c1aba2f079a7711575ae24f439b6]] [formerly 2d78b3f89fb5897f3afad247eddb77ade858efb7 [formerly 261bb0ffcaf80f6a2f42ae4cbb26f38ba5959ca6] [formerly 32e05072c7208e58b4f38cc1ad5acbc0637d763d [formerly 3e9d138f2fcba1d2f97f3b10124c29b923c59f3f]]] [formerly b1c6c22f9fd2d4caf6c32a4d488c909e7b460013 [formerly 040c069288b81900734717f0753ba8799731327e] [formerly 661324b26056be7914d55b24252bc71acf764e43 [formerly 157184dd29172482a156f24168b9d9cd33859093]] [formerly bbcd61d2eb03a2694d290ed46ebc416fb9a92eef [formerly 644ded442e4f5a0459abc3cda3b3d7f11bc6da67] [formerly 7fad748713f9bb77aa6dc931d745d7d2c5c5f220 [formerly 54b8b81]]]] Former-commit-id: c062bef06b3b8cbeca5a736fcd1958699802562d [formerly 4687f6362a3e89cae099ce74efb8aa9c71f3721e] [formerly 442947b6d711eb9b800e9a98c5a743b45f3ef2d9 [formerly b84170c13262873a83a1755b3db28977655fb98d]] [formerly fd22f645a6329919e14fd5c0d612961b709003cc [formerly 99dfb643dffa50a16fcd5bb5b4d4bd85cd50591e] [formerly 7769161054b82b61221542d328b05e298e8f85ef [formerly 722dcd33b58417b5a894f0906fe31dcc6cd9e1ab]]] Former-commit-id: a92701bf344b2fda0f75ad3fb93fcf7e0ff1c31a [formerly 8b79bc74c68edc52f423a73dedd3ed9ed440c056] [formerly 7d9d983ec31a6ebeb5af8658c78fdd4a9048cd06 [formerly 627b7e85592a0fa5855d0b16c4a7dee9042c2211]] Former-commit-id: b6e4800d9a2119e8a87826cbb634c3b16ad9ec5c [formerly cd98c6d262fde6634d46e0d13061317e9b50a3b6] Former-commit-id: 6253ef4461df409f79b14f9ee4802bdaf1a2ee76
1 parent 352e995 commit 57881a3

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

11. useful tools/11.01 pprint.ipynb

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# pprint 模块:打印 Python 对象"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"`pprint` 是 pretty printer 的缩写,用来打印 Python 数据结构,与 `print` 相比,它打印出来的结构更加整齐,便于阅读。"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"metadata": {
21+
"collapsed": true
22+
},
23+
"outputs": [],
24+
"source": [
25+
"import pprint"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {},
31+
"source": [
32+
"生成一个 Python 对象:"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 2,
38+
"metadata": {
39+
"collapsed": true
40+
},
41+
"outputs": [],
42+
"source": [
43+
"data = (\n",
44+
" \"this is a string\", \n",
45+
" [1, 2, 3, 4], \n",
46+
" (\"more tuples\", 1.0, 2.3, 4.5), \n",
47+
" \"this is yet another string\"\n",
48+
" )"
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"metadata": {},
54+
"source": [
55+
"使用普通的 `print` 函数:"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 3,
61+
"metadata": {
62+
"collapsed": false
63+
},
64+
"outputs": [
65+
{
66+
"name": "stdout",
67+
"output_type": "stream",
68+
"text": [
69+
"('this is a string', [1, 2, 3, 4], ('more tuples', 1.0, 2.3, 4.5), 'this is yet another string')\n"
70+
]
71+
}
72+
],
73+
"source": [
74+
"print data"
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"metadata": {},
80+
"source": [
81+
"使用 `pprint` 模块中的 `pprint` 函数:"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 4,
87+
"metadata": {
88+
"collapsed": false
89+
},
90+
"outputs": [
91+
{
92+
"name": "stdout",
93+
"output_type": "stream",
94+
"text": [
95+
"('this is a string',\n",
96+
" [1, 2, 3, 4],\n",
97+
" ('more tuples', 1.0, 2.3, 4.5),\n",
98+
" 'this is yet another string')\n"
99+
]
100+
}
101+
],
102+
"source": [
103+
"pprint.pprint(data)"
104+
]
105+
},
106+
{
107+
"cell_type": "markdown",
108+
"metadata": {},
109+
"source": [
110+
"可以看到,这样打印出来的公式更加美观。"
111+
]
112+
}
113+
],
114+
"metadata": {
115+
"kernelspec": {
116+
"display_name": "Python 2",
117+
"language": "python",
118+
"name": "python2"
119+
},
120+
"language_info": {
121+
"codemirror_mode": {
122+
"name": "ipython",
123+
"version": 2
124+
},
125+
"file_extension": ".py",
126+
"mimetype": "text/x-python",
127+
"name": "python",
128+
"nbconvert_exporter": "python",
129+
"pygments_lexer": "ipython2",
130+
"version": "2.7.6"
131+
}
132+
},
133+
"nbformat": 4,
134+
"nbformat_minor": 0
135+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
894018f8f865666dcd1949c6ca2e2795dca1343e
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0af58cc7fe38ffd2c53d95301789c3436d30789a

0 commit comments

Comments
 (0)