-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
216 lines (166 loc) · 4.87 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Category: python | Mike Tech]]></title>
<link href="http://mikegao.github.com/blog/categories/python/atom.xml" rel="self"/>
<link href="http://mikegao.github.com/"/>
<updated>2013-01-29T17:36:28+08:00</updated>
<id>http://mikegao.github.com/</id>
<author>
<name><![CDATA[Mike Gao]]></name>
<email><![CDATA[mike.gao0611@gmail.com]]></email>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[Beginner_Of_Python]]></title>
<link href="http://mikegao.github.com/blog/beginner-of-python/"/>
<updated>2013-01-29T10:42:00+08:00</updated>
<id>http://mikegao.github.com/blog/beginner-of-python</id>
<content type="html"><![CDATA[<h4>变量交换</h4>
<pre><code>x = 6
y = 5
x, y = y, x
print x
>>> 5
print y
>>> 6
</code></pre>
<h4>if 语句再行内</h4>
<pre><code>print "Hello" if True else "World"
>>> Hello
</code></pre>
<h4>连接</h4>
<p> 下面的最后一种方式在绑定两个不同类型的对象时显得很cool</p>
<pre><code>nfc = ["Packers", "49ers"]
afc = ["Ravens", "Patriots"]
print nfc + afc
>>> ['Packers', '49ers', 'Ravens', 'Patriots']
print str(1) + " world"
>>> 1 world
pring 1, "world"
>>> 1 world
print nfc, 1
>>> ['Packers', '49ers'] 1
</code></pre>
<h4>数字技巧</h4>
<pre><code># 除后向下取整
print 5.0//2
>>> 2
# 2的5次方
print 2**5
>>> 32
</code></pre>
<h4>注意浮点数的除法</h4>
<pre><code>print .3/.1
>>> 2.9999999999999996
print .3//.1
>>> 2.0
</code></pre>
<h4>数值比较</h4>
<pre><code>x = 2
if 3 > x > 1:
print x
>>> 2
if 1 < x > 0:
print x
>>> 2
</code></pre>
<h4>同时迭代两个列表</h4>
<pre><code>nfc = ['Packers', '49ers']
afc = ['Ravens', 'Patriots']
for teama, teamb in zip(nfc, afc)
print teama + " vs. " + teamb
>>> Packers vs. Ravens
>>> 49ers vs. Patriots
</code></pre>
<h4>带索引的列表迭代</h4>
<pre><code>teams = ["Packers", "49ers", "Ravens", "Patriots"]
for index, team in enumerate(teams):
print index, team
>>> 0 Packers
>>> 1 49ers
>>> 2 Ravens
>>> 3 Patriots
</code></pre>
<h4>列表推导式</h4>
<p> 已知一个列表,我们可以刷选出偶数列表方法</p>
<pre><code>numbers = [1,2,3,4,5]
even = [number for number in numbers if number%2 == 0]
</code></pre>
<h4>字典推导</h4>
<p> 和列表推导类似</p>
<pre><code>teams = ['Packers', '49ers', 'Ravens', 'Patriots']
print {key: value for value, key in enumerate(teams)}
>>> {'49ers': 1, 'Ravens': 2, 'Patriots': 3, 'Packers': 0}
</code></pre>
<h4>初始化列表的值</h4>
<pre><code>items = [0]*3
print items
>>> [0, 0, 0]
</code></pre>
<h4>列表转换为字符串</h4>
<pre><code>teams = ['Packers', '49ers', 'Ravens', 'Patriots']
print ", ".join(teams)
>>> 'Packers, 49ers, Ravens, Patriots'
</code></pre>
<h4>从字典中获取元素</h4>
<pre><code>data = {'user': 1, 'name': 'Max', 'three': 4}
try:
is_admin = data['admin']
except KeyError:
is_admin = False
</code></pre>
<p> 替换成这样:</p>
<pre><code>data = {'user': 1, 'name': 'Max', 'three': 4}
is_admin = data.get('admin', False)
</code></pre>
<h4>获取列表的子集</h4>
<pre><code>x= [1,2,3,4,5]
# 前3个
print x[:3]
>>> [1, 2, 3]
# 中间4个
print x[1:5]
>>> [2, 3, 4, 5]
# 最后3个
print x[3:]
>>> [4, 5, 6]
# 奇数项
print x[::2]
>> [1, 3, 5]
# 偶数项
print x[1::2]
>> [2, 4, 6]
</code></pre>
<h4>60个字符解决FizzBuzz</h4>
<p> 写一个程序,打印数字1到100,3的倍数打印“Fizz”来替换这个数,5的倍数打印“Buzz”,对于既是3的倍数又是5的倍数的数字打印“FizzBuzz”</p>
<pre><code>for x in range(101):print'fizz'[x%3*4::]+'buzz'[x%5*4::]or x
</code></pre>
<h4>集合</h4>
<pre><code>from collections import Counter
print Counter("hello")
>>> Counter({'1': 2, 'h': 1, 'e': 1, 'o': 1})
</code></pre>
<h4>迭代工具</h4>
<p> 和collections库一样,还有一个库叫itertools,对某些问题真能高效地解决。其中一个用例是查找所有组合,他能告诉你在一个组中元素的所有不能的组合方式</p>
<pre><code>from itertools import combinations
teams = ["Packers", "49ers", "Ravens", "Patriots"]
for game in combinations(teams, 2):
print game
>>> ('Packers', '49ers')
>>> ('Packers', 'Ravens')
>>> ('Packers', 'Patriots')
>>> ('49ers', 'Ravens')
>>> ('49ers', 'Patriots')
>>> ('Ravens', 'Patriots')
</code></pre>
<h4>False == True</h4>
<pre><code>False = True
if False:
print "Hello"
else:
print "World"
>>> Hello
</code></pre>
]]></content>
</entry>
</feed>