Skip to content

Commit 54b8b81

Browse files
committed
添加新内容
Former-commit-id: fbd6818
1 parent c2aa435 commit 54b8b81

File tree

3 files changed

+836
-0
lines changed

3 files changed

+836
-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+
}

0 commit comments

Comments
 (0)