-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
349 lines (165 loc) · 162 KB
/
search.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>音乐播放器</title>
<link href="/2019/07/22/%E9%9F%B3%E4%B9%90%E6%92%AD%E6%94%BE%E5%99%A8/"/>
<url>/2019/07/22/%E9%9F%B3%E4%B9%90%E6%92%AD%E6%94%BE%E5%99%A8/</url>
<content type="html"><![CDATA[<p>我写的还有一些问题:<br>播放一首歌之后不能播放第二首<br>请见谅</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pygame</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">welcome</span><span class="params">()</span>:</span></span><br><span class="line"> print(<span class="string">'''</span></span><br><span class="line"><span class="string"> *************************</span></span><br><span class="line"><span class="string"> * 欢迎来到酷我音乐播放器 *</span></span><br><span class="line"><span class="string"> *************************</span></span><br><span class="line"><span class="string"> '''</span>)</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">select</span><span class="params">()</span>:</span></span><br><span class="line"> print(<span class="string">''' </span></span><br><span class="line"><span class="string"> **************************</span></span><br><span class="line"><span class="string"> * 欢迎来到酷我音乐播放器! * </span></span><br><span class="line"><span class="string"> ************************** </span></span><br><span class="line"><span class="string"> * 1.播放 2.暂停 *</span></span><br><span class="line"><span class="string"> * 3.下一曲 4.上一曲 *</span></span><br><span class="line"><span class="string"> * 5.增加音量 6.减少音量 *</span></span><br><span class="line"><span class="string"> * 7.取消暂停 0.退出 *</span></span><br><span class="line"><span class="string"> ***************************</span></span><br><span class="line"><span class="string"> '''</span>)</span><br><span class="line"> <span class="keyword">return</span> input(<span class="string">"请选择您要操作的选项:"</span>)</span><br><span class="line"><span class="comment">#开始播放</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">playMusic</span><span class="params">(path)</span>:</span></span><br><span class="line"> pygame.mixer.music.load(path)</span><br><span class="line"> pygame.mixer.music.play(<span class="number">1</span>)</span><br><span class="line"> <span class="comment"># print("000000000000000000000000")</span></span><br><span class="line"> <span class="comment"># # pygame.mixer.music.queue(r"E:\python\day02\作业\韩红,林俊杰 - 飞云之下.mp3")</span></span><br><span class="line"> <span class="comment"># # # print("4444444444")</span></span><br><span class="line"> x = <span class="number">1</span></span><br><span class="line"><span class="comment">#暂停播放</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">pasueMusic</span><span class="params">()</span>:</span></span><br><span class="line"> pygame.mixer.music.pause()</span><br><span class="line"></span><br><span class="line"><span class="comment">#取消暂停</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">unpasueMusic</span><span class="params">()</span>:</span></span><br><span class="line"> pygame.mixer.music.unpause()</span><br><span class="line"><span class="comment">#控制音量</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">switchMusic</span><span class="params">()</span>:</span></span><br><span class="line"> print(<span class="string">"12345423"</span>)</span><br><span class="line"> pygame.mixer.music.load(path[x])</span><br><span class="line"> pygame.mixer.music.play()</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">increaseMusic</span><span class="params">()</span>:</span></span><br><span class="line"> pygame.mixer.music.set_volume()</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"> <span class="comment">#为歌曲所在目录</span></span><br><span class="line">path2 = <span class="string">r"E:\python\day02\歌曲"</span></span><br><span class="line">path1= os.listdir(<span class="string">r"E:\python\day02\歌曲"</span>)</span><br><span class="line">path = []</span><br><span class="line"><span class="comment">#遍历路径并拼接起来</span></span><br><span class="line"><span class="keyword">for</span> filename <span class="keyword">in</span> path1:</span><br><span class="line"> path.append(os.path.join(path2,filename))</span><br><span class="line">pygame.mixer.init()</span><br><span class="line">x = <span class="number">0</span></span><br><span class="line">y = <span class="number">5</span></span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"> <span class="comment"># welcome()</span></span><br><span class="line"> n = select()</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> n == <span class="string">"1"</span>:</span><br><span class="line"> playMusic(path[x])</span><br><span class="line"> print(<span class="string">"当前播放的歌曲为"</span>,os.path.basename(path[x]))</span><br><span class="line"> <span class="keyword">elif</span> n ==<span class="string">"2"</span>:</span><br><span class="line"> pasueMusic()</span><br><span class="line"> <span class="keyword">elif</span> n == <span class="string">"3"</span> :</span><br><span class="line"> <span class="keyword">if</span> x != <span class="number">6</span>:</span><br><span class="line"> x += <span class="number">1</span></span><br><span class="line"> <span class="comment"># print(x)</span></span><br><span class="line"> pygame.mixer.music.stop()</span><br><span class="line"> switchMusic()</span><br><span class="line"> print(<span class="string">"当前播放的歌曲为"</span>,os.path.basename(path[x]))</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> x =<span class="number">6</span></span><br><span class="line"> print(<span class="string">"最后一首!继续播放这一首"</span>)</span><br><span class="line"> print(<span class="string">"当前播放的歌曲为"</span>,os.path.basename(path[x]))</span><br><span class="line"> <span class="keyword">elif</span> n == <span class="string">"4"</span>:</span><br><span class="line"> <span class="keyword">if</span> x != <span class="number">0</span>:</span><br><span class="line"> x -= <span class="number">1</span></span><br><span class="line"> print(x)</span><br><span class="line"> pygame.mixer.music.stop()</span><br><span class="line"> switchMusic()</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"继续播放这一首"</span>)</span><br><span class="line"> <span class="keyword">elif</span> n == <span class="string">"5"</span> :</span><br><span class="line"> y += <span class="number">2</span></span><br><span class="line"> <span class="keyword">if</span> y <= <span class="number">10</span>:</span><br><span class="line"> pygame.mixer.music.set_volume(y/<span class="number">10</span>)</span><br><span class="line"> <span class="comment"># print("1111111111",y)</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> y = <span class="number">10</span></span><br><span class="line"> <span class="comment"># print("22222222222",y)</span></span><br><span class="line"> pygame.mixer.music.set_volume(y/<span class="number">10</span>)</span><br><span class="line"> <span class="keyword">elif</span> n == <span class="string">"6"</span>:</span><br><span class="line"> y -= <span class="number">2</span></span><br><span class="line"> <span class="keyword">if</span> y >= <span class="number">0</span>:</span><br><span class="line"> <span class="comment"># print("333",y)</span></span><br><span class="line"> pygame.mixer.music.set_volume(y/<span class="number">10</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> y=<span class="number">0</span></span><br><span class="line"> <span class="comment"># print("444",y)</span></span><br><span class="line"> pygame.mixer.music.set_volume(y/<span class="number">10</span>)</span><br><span class="line"> <span class="keyword">elif</span> n == <span class="string">"7"</span>:</span><br><span class="line"> unpasueMusic()</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">break</span></span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>python输出及其运算</title>
<link href="/2019/07/21/python%E8%BE%93%E5%87%BA%E5%8F%8A%E5%85%B6%E8%BF%90%E7%AE%97/"/>
<url>/2019/07/21/python%E8%BE%93%E5%87%BA%E5%8F%8A%E5%85%B6%E8%BF%90%E7%AE%97/</url>
<content type="html"><![CDATA[<h2 id="格式化输出"><a href="#格式化输出" class="headerlink" title="格式化输出"></a>格式化输出</h2><p>%s 字符串<br>%ns n代表字符串占据的宽度<br>%-ns -代表左对齐,默认为右对齐</p><p>%d int类型<br>%02d 0:占位符 2:长度</p><p>%f 浮点型<br>%nf 小数点后保留的位数</p><h2 id="分支语句"><a href="#分支语句" class="headerlink" title="分支语句"></a>分支语句</h2><h4 id="单分支"><a href="#单分支" class="headerlink" title="单分支"></a>单分支</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">if 判断条件:</span><br><span class="line"> 语句</span><br></pre></td></tr></table></figure><h4 id="双分支"><a href="#双分支" class="headerlink" title="双分支"></a>双分支</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 判断条件:</span><br><span class="line"> 语句块<span class="number">1</span></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> 语句块<span class="number">2</span></span><br></pre></td></tr></table></figure><h4 id="多分支"><a href="#多分支" class="headerlink" title="多分支"></a>多分支</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">if 判断条件1:</span><br><span class="line"> 语句块1</span><br><span class="line">elif:</span><br><span class="line"> 语句块2</span><br><span class="line">...</span><br><span class="line">else:</span><br><span class="line"> 语句块n</span><br></pre></td></tr></table></figure><h2 id="运算"><a href="#运算" class="headerlink" title="运算"></a>运算</h2><h3 id="身份运算"><a href="#身份运算" class="headerlink" title="身份运算"></a>身份运算</h3><p>is 判断两个标识符是否引用同一个对象,若是返回True,否则返回False<br>not is </p><h3 id="成员运算"><a href="#成员运算" class="headerlink" title="成员运算"></a>成员运算</h3><p>in 判断指定的元素是否存在于指定的序列中,若存在则返回True,不存在则返回False<br> not in</p><h3 id="位运算"><a href="#位运算" class="headerlink" title="位运算"></a>位运算</h3><p>& 按位与<br>| 按位或<br>^ 按位异或<br>~ 按位取反<br><< 左移</p><p>特别注意使用位运算的时候一定要将数值转为2进制来计算</p><h3 id="逻辑运算"><a href="#逻辑运算" class="headerlink" title="逻辑运算"></a>逻辑运算</h3><p>and 与运算 全真则真,一假则假<br>or 或运算 一真则真,全假则假<br>not 非运算 真变假,假变真</p><p>注意:使用and来连接的时候,若有一个表达式的结果为假,则后面的表达式不再进行运算直接返回假<br>使用or来连接的时候,若有一个表达式的结果为真时,则后面的表达式不再运算直接返回真<br>优先级:( ) > not > and >or</p><h3 id="比较运算"><a href="#比较运算" class="headerlink" title="比较运算"></a>比较运算</h3><p>== 等于<br>!= 不等于</p><blockquote><p>大于<br>< 小于<br>= 大于等于<br><= 小于等于<br>使用比较运算符时,得到的时一个bool值</p></blockquote><h3 id="赋值运算"><a href="#赋值运算" class="headerlink" title="赋值运算"></a>赋值运算</h3><p>+= -= <em>= /= %= *</em>= //=<br>注意:使用赋值运算的时候,卸载等号左边的变量的值会发生变化,写在等号右边的值不发生变化,只参与运算</p>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>列表及其常用方法</title>
<link href="/2019/07/21/%E5%88%97%E8%A1%A8%E5%8F%8A%E5%85%B6%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0/"/>
<url>/2019/07/21/%E5%88%97%E8%A1%A8%E5%8F%8A%E5%85%B6%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0/</url>
<content type="html"><![CDATA[<h1 id="列表及其常用函数"><a href="#列表及其常用函数" class="headerlink" title="列表及其常用函数"></a>列表及其常用函数</h1><h2 id="1-定义"><a href="#1-定义" class="headerlink" title="1. 定义:"></a>1. 定义:</h2><p>有序的集合</p><h2 id="2-创建列表:"><a href="#2-创建列表:" class="headerlink" title="2.创建列表:"></a>2.创建列表:</h2><blockquote><p>列表名=[元素1,元素2,….]</p></blockquote><p>元素:可以试任意类型<br>元素与元素使用逗号隔开</p><h2 id="3-列表的操作"><a href="#3-列表的操作" class="headerlink" title="3.列表的操作"></a>3.列表的操作</h2><h4 id="1-列表的组合"><a href="#1-列表的组合" class="headerlink" title="1.列表的组合"></a>1.列表的组合</h4><blockquote><p>list1 = list2 + list3</p></blockquote><p>功能:将list1中的元素与list2中的元素组合成一个新的列表并返回</p><h4 id="2-列表的重复"><a href="#2-列表的重复" class="headerlink" title="2.列表的重复"></a>2.列表的重复</h4><blockquote><p>list2 = list *n</p></blockquote><p>功能:将list1中的元素重复n次输出到list2中</p><h4 id="3-列表的访问"><a href="#3-列表的访问" class="headerlink" title="3.列表的访问"></a>3.列表的访问</h4><blockquote><p>list1[index]</p></blockquote><p>index 取值范围:[0,len(list1)<br>可以取负值,若为负,则从右往左取值</p><h4 id="4-列表的截取"><a href="#4-列表的截取" class="headerlink" title="4.列表的截取"></a>4.列表的截取</h4><blockquote><p>list1[start:end:step]</p></blockquote><h4 id="5-二维列表"><a href="#5-二维列表" class="headerlink" title="5.二维列表"></a>5.二维列表</h4><blockquote><p>list3 =[list1,list2]</p></blockquote><p>注意:当列表中的元素刚好为列表的时候,我们可以称之为二维列表</p><h2 id="列表的常用函数"><a href="#列表的常用函数" class="headerlink" title="列表的常用函数"></a>列表的常用函数</h2><h4 id="1-添加元素"><a href="#1-添加元素" class="headerlink" title="1.添加元素"></a>1.添加元素</h4><blockquote><p>list。append(obj)</p></blockquote><h4 id="2-将序列打碎插入"><a href="#2-将序列打碎插入" class="headerlink" title="2.将序列打碎插入"></a>2.将序列打碎插入</h4><blockquote><p>list1.extend(序列)</p></blockquote><h4 id="3-删除指定下标处的元素"><a href="#3-删除指定下标处的元素" class="headerlink" title="3.删除指定下标处的元素"></a>3.删除指定下标处的元素</h4><blockquote><p>list.pop(index)</p></blockquote><h4 id="4-指定下标插入指定的元素"><a href="#4-指定下标插入指定的元素" class="headerlink" title="4.指定下标插入指定的元素"></a>4.指定下标插入指定的元素</h4><blockquote><p>list1.insert(index,obj)</p></blockquote><h4 id="5-函数第一次匹配到的元素"><a href="#5-函数第一次匹配到的元素" class="headerlink" title="5.函数第一次匹配到的元素"></a>5.函数第一次匹配到的元素</h4><blockquote><p>list1.remove(obj)</p></blockquote><h4 id="6-清空列表"><a href="#6-清空列表" class="headerlink" title="6.清空列表"></a>6.清空列表</h4><blockquote><p>list.clear</p></blockquote><h4 id="7-删除列表"><a href="#7-删除列表" class="headerlink" title="7.删除列表"></a>7.删除列表</h4><blockquote><p>del list</p></blockquote><h4 id="8-统计指定元素出现的次数"><a href="#8-统计指定元素出现的次数" class="headerlink" title="8.统计指定元素出现的次数"></a>8.统计指定元素出现的次数</h4><blockquote><p>list2.count(obj)</p></blockquote><h4 id="9-返回列表中最大-最小值"><a href="#9-返回列表中最大-最小值" class="headerlink" title="9.返回列表中最大/最小值"></a>9.返回列表中最大/最小值</h4><blockquote><p>max(list1) / min(list1)</p></blockquote><h4 id="10-倒序列表"><a href="#10-倒序列表" class="headerlink" title="10.倒序列表"></a>10.倒序列表</h4><blockquote><p>list1.reverse()</p></blockquote><h4 id="11-将列表排序"><a href="#11-将列表排序" class="headerlink" title="11.将列表排序"></a>11.将列表排序</h4><blockquote><p>list1.sort()</p></blockquote><p>默认升序 ,reverse =True 降序</p>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>文件操作</title>
<link href="/2019/07/21/python%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C/"/>
<url>/2019/07/21/python%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C/</url>
<content type="html"><![CDATA[<h2 id="读写文件"><a href="#读写文件" class="headerlink" title="读写文件"></a>读写文件</h2><p>普通程序读写文件的时候需要向操作系统发出请求,请求操作系统打开一个文件对象,通过这个文件对象来进行读写</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">with</span> open(str1,<span class="string">"r"</span>,encoding=<span class="string">"utf-8"</span>) <span class="keyword">as</span> f:</span><br><span class="line"> <span class="keyword">pass</span></span><br></pre></td></tr></table></figure><blockquote><p>f =open(path,mode,encoding,errors)</p></blockquote><p>path:文件路径<br>mode:模式 r:读取,默认<br>encoding:编码格式<br>errors默认strict[严格的] ignore 忽略编码错误<br>f:打开的文件对象,f是一个迭代f器</p><blockquote><p>f.read()</p></blockquote><p>一次性读取所有内容</p><blockquote><p>f.read(size)</p></blockquote><p>一次性读取size字节</p><blockquote><p>f.readlines()</p></blockquote><p>一次性读取所有行</p><blockquote><p>f.readline()</p></blockquote><p>一次性读取一行</p><blockquote><p>f.close()</p></blockquote><p>关闭文件<br>当使用文件读写时,需要在结尾使用f.close语句,不然会出现错误</p><h2 id="with语句"><a href="#with语句" class="headerlink" title="with语句"></a>with语句</h2><p>语法:<br>with open(path,mode,encoding) as 文件名:<br> 对文件进行操作</p><p>当文件一旦关闭,不能对此文件进行操作。</p><h2 id="读取2进制文件with"><a href="#读取2进制文件with" class="headerlink" title="读取2进制文件with"></a>读取2进制文件with</h2><blockquote><p>open(path,mode) as f:<br> f.read()</p></blockquote><p>mode: rb 读取二进制文件</p><p>结论:普通文件可以使用r/rb来进行读取,但是二进制的文件<br>只能使用rb的方式来进行读取。</p><p>二进制文件:图片,视频,音频</p><blockquote><p>open(path,mode,encoding)</p></blockquote><p>path:路径<br>mode:w/a<br>encoding:写入编码格式</p><blockquote><p>f.write(str1)</p></blockquote><p>写入的必须是字符串</p><p>使用w的方式写入:当path存在的时候,则覆盖,若path不存在<br>则创建。</p><p>使用a的方式写入的时候,若path存在,则追加,若path不存在,<br>则创建</p><blockquote><p>f.writelines([“hello”,”good”,”nice”])</p></blockquote><p>将列表中的字符串一次性写入到文件中</p><h2 id="内存中读写"><a href="#内存中读写" class="headerlink" title="内存中读写"></a>内存中读写</h2><p>StingIO</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> io <span class="keyword">import</span> StringIO</span><br><span class="line"><span class="keyword">from</span> io <span class="keyword">import</span> BytesIO</span><br><span class="line"></span><br><span class="line">bio = BytesIO()</span><br><span class="line"><span class="comment"># 要求写入的必须是二进制的</span></span><br><span class="line">bio.write(<span class="string">"中国"</span>.encode())</span><br><span class="line"><span class="comment"># 查看bio内容</span></span><br><span class="line">print(bio.getvalue().decode())</span><br></pre></td></tr></table></figure><h2 id="csv文件读写"><a href="#csv文件读写" class="headerlink" title="csv文件读写"></a>csv文件读写</h2><p>#-<em>- author:zhangjiao -</em>-<br>import csv</p><p>‘’’<br>需求:封装一下csv文件的读取,要求读取前n行,并且读取的数据以列表的方式返回。</p><p>‘’’</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">readcsv</span><span class="params">(path,n)</span>:</span></span><br><span class="line"> <span class="keyword">with</span> open(path,<span class="string">"r"</span>,encoding=<span class="string">"gbk"</span>,errors=<span class="string">"ignore"</span>) <span class="keyword">as</span> f:</span><br><span class="line"> csv_reader = csv.reader(f)</span><br><span class="line"> datalist = []</span><br><span class="line"> <span class="keyword">for</span> x <span class="keyword">in</span> range(n):</span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> datalist.append(next(csv_reader))</span><br><span class="line"> <span class="keyword">except</span> StopIteration <span class="keyword">as</span> e:</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> datalist</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">readandwrite</span><span class="params">(path1,path2,n)</span>:</span></span><br><span class="line"> <span class="keyword">with</span> open(path1,<span class="string">"r"</span>,encoding=<span class="string">"gbk"</span>) <span class="keyword">as</span> f:</span><br><span class="line"> <span class="keyword">with</span> open(path2,<span class="string">"w"</span>,encoding=<span class="string">"gbk"</span>) <span class="keyword">as</span> f2:</span><br><span class="line"> csv_reader = csv.reader(f)</span><br><span class="line"> csv_writer = csv.writer(f2)</span><br><span class="line"> <span class="keyword">for</span> x <span class="keyword">in</span> range(n):</span><br><span class="line"> csv_writer.writerow(next(csv_reader))</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"></span><br><span class="line"> res = readcsv(<span class="string">"002.csv"</span>,<span class="number">5</span>)</span><br><span class="line"> print(res)</span><br><span class="line"> <span class="keyword">with</span> open(<span class="string">"002.csv"</span>,<span class="string">"r"</span>,encoding=<span class="string">"gbk"</span>) <span class="keyword">as</span> f:</span><br><span class="line"> csv_reader = csv.reader(f)</span><br><span class="line"> print(next(csv_reader))</span><br><span class="line"> <span class="keyword">with</span> open(<span class="string">"0002.csv"</span>,<span class="string">"w"</span>,encoding=<span class="string">"gbk"</span>) <span class="keyword">as</span> f2:</span><br><span class="line"> csv_writer = csv.writer(f2)</span><br><span class="line"> <span class="keyword">for</span> row <span class="keyword">in</span> res:</span><br><span class="line"> csv_writer.writerow(row)</span><br><span class="line"></span><br><span class="line"> readandwrite(<span class="string">"002.csv"</span>,<span class="string">"1.csv"</span>,<span class="number">50</span>)</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>栈</title>
<link href="/2019/07/21/%E4%BD%BF%E7%94%A8%E6%A0%88%E6%9D%A5%E9%81%8D%E5%8E%86%E7%9B%AE%E5%BD%95/"/>
<url>/2019/07/21/%E4%BD%BF%E7%94%A8%E6%A0%88%E6%9D%A5%E9%81%8D%E5%8E%86%E7%9B%AE%E5%BD%95/</url>
<content type="html"><![CDATA[<h1 id="使用栈来遍历目录"><a href="#使用栈来遍历目录" class="headerlink" title="使用栈来遍历目录"></a>使用栈来遍历目录</h1><a id="more"></a><pre><code class="python"><span class="keyword">import</span> os<span class="function"><span class="keyword">def</span> <span class="title">getdirstack</span><span class="params">(path)</span>:</span> <span class="comment"># 创建一个栈</span> stack = [] <span class="comment"># 先让根路径压栈</span> stack.append(path) <span class="comment"># 判断栈是否为空</span> <span class="keyword">while</span> stack: print(stack) <span class="comment"># 出栈</span> filepath = stack.pop() <span class="comment"># print(filepath)</span> <span class="comment"># 列举指定目录下所有的目录及文件</span> fileList = os.listdir(filepath) print(fileList) <span class="keyword">for</span> filename <span class="keyword">in</span> fileList: <span class="comment"># 获取绝对路径</span> abspath = os.path.join(filepath,filename) <span class="comment"># 若是目录</span> <span class="keyword">if</span> os.path.isdir(abspath): print(<span class="string">"目录:"</span>,filename) stack.append(abspath) <span class="comment"># pass</span> <span class="keyword">else</span>: print(<span class="string">"文件:"</span>,filename) <span class="comment"># pass</span>path = <span class="string">r"/PycharmProjects"</span>getdirstack(path)</code></pre>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>os模块</title>
<link href="/2019/07/21/os%E6%A8%A1%E5%9D%97/"/>
<url>/2019/07/21/os%E6%A8%A1%E5%9D%97/</url>
<content type="html"><![CDATA[<h1 id="os模块"><a href="#os模块" class="headerlink" title="os模块"></a>os模块</h1><p>os模块是一个python内置的文件模块</p><h2 id="os模块的使用"><a href="#os模块的使用" class="headerlink" title="os模块的使用"></a>os模块的使用</h2><p><code>import os</code></p><p><code>os.getcwd()</code><br>功能:获取当前目录的绝对路径</p><p>绝对路径:<br>window:以盘符开头,或者以\开头<br>linux:以/开头或者以~开头<br>相对路径:<br>window:以文件开头,以./开头 或者../开头<br>linux:与window相同</p><a id="more"></a><p><code>os.listdir(path)</code><br>功能:显示指定路径下所有的文件名以及目录名,若path不指定,默认当前目录下。</p><p><code>os.path.abspath(path)</code><br>功能:将指定的路径拼接到当前所在所在的路径下</p><p><code>os.path.split(path)</code><br>功能:返回指定路径目录部分以及文件部分<br>本质:以最后一个/来进行切分,返回两个值,前面的就是目录部分,后面的就是文件部分<br>若是”.”切分到文件部分去</p><p><code>os.path.dirname(path)</code><br>功能:返回指定路径的目录部分</p><p><code>os.path.basename(path)</code><br>功能:返回指定路径的文件部分</p><p><code>os.path.join(path,paths)</code><br>功能:对路径进行拼接处理<br>paths:可以接收多个路径<br>若paths存在绝对路径,前面路径则不保留。</p><p><code>os.path.getsize(path)</code><br>功能:获取指定路径文件的大小【目录的大小获取不到】</p><p><code>os.path.exists(path)</code><br>功能:判断指定的路径是否存在,若存在则返回True,否则返回False</p><p><code>os.path.isdir(path)</code><br>功能:判断该路径是否为目录,若是返回True,否则返回False</p><p><code>os.path.isfile(path)</code><br>功能:判断指定的路径是否为文件,若是则返回True,否则返回False</p><p><code>os.remove(path)</code><br>功能:删除指定的路径的文件</p><p><code>os.mkdir(path)</code><br>功能:创建指定的目录</p><p><code>os.makedirs(path)</code><br>功能:递归创建多个目录</p><p><code>os.rmdir(path)</code><br>功能:删除指定的空目录</p><p><code>os.removedirs(path)</code><br>功能:递归删除目录</p><p><code>os.chdir(path)</code><br>功能:切换目录</p>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>队列</title>
<link href="/2019/07/21/%E4%BD%BF%E7%94%A8%E9%98%9F%E5%88%97%E6%9D%A5%E9%81%8D%E5%8E%86%E7%9B%AE%E5%BD%95/"/>
<url>/2019/07/21/%E4%BD%BF%E7%94%A8%E9%98%9F%E5%88%97%E6%9D%A5%E9%81%8D%E5%8E%86%E7%9B%AE%E5%BD%95/</url>
<content type="html"><![CDATA[<h1 id="使用队列来遍历目录"><a href="#使用队列来遍历目录" class="headerlink" title="使用队列来遍历目录"></a>使用队列来遍历目录</h1><a id="more"></a><pre><code class="python"><span class="keyword">import</span> collections<span class="keyword">import</span> os<span class="function"><span class="keyword">def</span> <span class="title">getAllDirQueue</span><span class="params">(path)</span>:</span> <span class="comment"># 创建队列</span> queue = collections.deque() queue.append(path) <span class="keyword">while</span> queue: <span class="comment"># 出队</span> filepath = queue.popleft() fileList = os.listdir(filepath) <span class="keyword">for</span> filename <span class="keyword">in</span> fileList: absPath = os.path.join(filepath,filename) <span class="keyword">if</span> os.path.isdir(absPath): print(<span class="string">"目录:"</span>,filename) queue.append(absPath) <span class="keyword">else</span>: print(<span class="string">"文件:"</span>,filename)<span class="comment">#输入路径</span>path = <span class="string">r"/Users/Pycharm"</span>getAllDirQueue(path)</code></pre>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>函数</title>
<link href="/2019/07/21/%E5%87%BD%E6%95%B0/"/>
<url>/2019/07/21/%E5%87%BD%E6%95%B0/</url>
<content type="html"><![CDATA[<h1 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h1><h2 id="函数参数:"><a href="#函数参数:" class="headerlink" title="函数参数:"></a>函数参数:</h2><h3 id="1-位置参数又称必选参数"><a href="#1-位置参数又称必选参数" class="headerlink" title="1.位置参数又称必选参数"></a>1.位置参数又称必选参数</h3><p>传递参数的时候,必须按照函数定义的顺序以及个数来进行传参。</p><h3 id="2-关键字参数"><a href="#2-关键字参数" class="headerlink" title="2.关键字参数"></a>2.关键字参数</h3><p>指传递参数的过程中,通过键值对的方式直接给形参指定具体的值,使用这种方式进行传参,可以清除位置关系<br>关键字参数与位置参数<br>可以同时存在,但是,若同时存在的时候,位置参数要写在前面<br>关键字参数要写在后面。</p><p>写在参数列表前面的形参若需使用关键字传参,<br>那么后面的参数一定也要使用关键字传参</p><a id="more"></a><h3 id="3-默认参数"><a href="#3-默认参数" class="headerlink" title="3.默认参数"></a>3.默认参数</h3><p>在定义函数的时候,我们可以给我们的函数提供一个默认值,若需要更改的时候,<br>我们传递该参数,若不需要更改的时候,我们不传递,若不传递,则使用提供默认值。</p><h3 id="4-不定长参数"><a href="#4-不定长参数" class="headerlink" title="4.不定长参数"></a>4.不定长参数</h3><p>定义函数的时候,有时候不知道用户要传递多少个参数进来,此时,我们可以使用不定长<br>参数来进行处理<br>特点:<br>能够处理比声明的更多的参数,传递多少参数,则处理多少参数,不传递则不处理。<br>包裹位置参数<br>收集除位置参数之外的剩余的必选参数,作为元组来进行处理。<br>使用包裹位置参数的时候,定义的时候只需要在形参前面添加一个<em>即可。<br>一般情况下使用</em> **args</p><p>若位置参数与包裹位置参数同时存在的情况下,<br>我们需要将包裹位置参数写在位置参数的后面</p><p>包裹关键字参数<br>收集除位置参数,以及默认参数以外剩余的关键字参数,作为字典来进行处理<br>定义包裹关键字参数的时候使用**<br>一般使用**kwargs</p><p>若在python定义函数的过程中,位置参数,默认参数,包裹位置参数以及包裹关键字<br>参数同时存在的情况下,<br>一般情况下顺序:位置参数,默认参数,包裹位置参数,包裹关键字参数<br>当然,默认参数与包裹位置参数之间顺序可以调换</p><h2 id="万能参数"><a href="#万能参数" class="headerlink" title="万能参数"></a>万能参数</h2><blockquote><p>def func(<em>args,*</em>kwargs):<br> pass</p></blockquote><h2 id="匿名函数"><a href="#匿名函数" class="headerlink" title="匿名函数"></a>匿名函数</h2><p>概念:指的是一类无需定义标识符的函数或者子程序<br>优点:没有名字,所以不会引起函数名冲突</p><h3 id="语法:"><a href="#语法:" class="headerlink" title="语法:"></a>语法:</h3><p>lambda 参数列表: 表达式</p><p>1.lambda只是一个表达式,函数体非常简单<br>2.lambda 匿名函数<br>参数与参数之间使用逗号隔开,冒号不能省略<br>3.表达式只能有一条语句,此函数无需return,表达式的结果就是函数的返回值</p><h3 id="调用匿名函数:"><a href="#调用匿名函数:" class="headerlink" title="调用匿名函数:"></a>调用匿名函数:</h3><p>将lambda表达式赋值给一个变量,通过调用这个变量来进行调用此匿名函数。</p><p>匿名函数适用于比较简单一些运算。</p><h2 id="偏函数"><a href="#偏函数" class="headerlink" title="偏函数"></a>偏函数</h2><p>将函数中的某些参数给固定住,然后返回一个新的函数,<br>调用新的函数会更加的方便。<br>作用:方便函数的调用。<br>偏函数是functools模块中提供的一个功能。</p><blockquote><p>functools.partial(func,要固定参数)</p></blockquote><p>参数一:被固定的函数的函数名<br>参数二:要固定的参数,可以以键值对的方式传参也可以以位置参数传参</p><p>功能:相等于给某个函数设置默认值,调用函数的时候会更加的方便。</p><h2 id="回调函数:"><a href="#回调函数:" class="headerlink" title="回调函数:"></a>回调函数:</h2><p>将函数作为参数传递到另外一个函数中,这个被传递进去后来又<br>被调用的函数我们称之为回调函数</p><h2 id="递归函数:"><a href="#递归函数:" class="headerlink" title="递归函数:"></a>递归函数:</h2><p>函数的内部可以调用其他的函数,但是若调用的这个函数正好是他自己本身的时候,<br>我们称这个函数为递归函数。</p><p>不建议使用递归<br>1.考虑栈溢出<br>2.递归性能不如循环</p><p>思路:<br>1.写出临界条件<br>2.找出这次与上次的关系<br>3.假设此函数已经可以使用,通过上次的结果计算本次的结果</p>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>列表及其常用函数</title>
<link href="/2019/07/21/%E5%88%97%E8%A1%A8%E5%8F%8A%E5%85%B6%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%20/"/>
<url>/2019/07/21/%E5%88%97%E8%A1%A8%E5%8F%8A%E5%85%B6%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%20/</url>
<content type="html"><![CDATA[<h1 id="列表及其常用函数"><a href="#列表及其常用函数" class="headerlink" title="列表及其常用函数"></a>列表及其常用函数</h1><h2 id="1-定义"><a href="#1-定义" class="headerlink" title="1. 定义:"></a>1. 定义:</h2><p>有序的集合</p><h2 id="2-创建列表:"><a href="#2-创建列表:" class="headerlink" title="2.创建列表:"></a>2.创建列表:</h2><blockquote><p>列表名=[元素1,元素2,….]</p></blockquote><p>元素:可以试任意类型<br>元素与元素使用逗号隔开</p><h2 id="3-列表的操作"><a href="#3-列表的操作" class="headerlink" title="3.列表的操作"></a>3.列表的操作</h2><h4 id="1-列表的组合"><a href="#1-列表的组合" class="headerlink" title="1.列表的组合"></a>1.列表的组合</h4><blockquote><p>list1 = list2 + list3</p></blockquote><p>功能:将list1中的元素与list2中的元素组合成一个新的列表并返回</p><h4 id="2-列表的重复"><a href="#2-列表的重复" class="headerlink" title="2.列表的重复"></a>2.列表的重复</h4><blockquote><p>list2 = list *n</p></blockquote><p>功能:将list1中的元素重复n次输出到list2中</p><h4 id="3-列表的访问"><a href="#3-列表的访问" class="headerlink" title="3.列表的访问"></a>3.列表的访问</h4><blockquote><p>list1[index]</p></blockquote><p>index 取值范围:[0,len(list1)<br>可以取负值,若为负,则从右往左取值</p><h4 id="4-列表的截取"><a href="#4-列表的截取" class="headerlink" title="4.列表的截取"></a>4.列表的截取</h4><blockquote><p>list1[start:end:step]</p></blockquote><h4 id="5-二维列表"><a href="#5-二维列表" class="headerlink" title="5.二维列表"></a>5.二维列表</h4><blockquote><p>list3 =[list1,list2]</p></blockquote><p>注意:当列表中的元素刚好为列表的时候,我们可以称之为二维列表</p><h2 id="列表的常用函数"><a href="#列表的常用函数" class="headerlink" title="列表的常用函数"></a>列表的常用函数</h2><h4 id="1-添加元素"><a href="#1-添加元素" class="headerlink" title="1.添加元素"></a>1.添加元素</h4><blockquote><p>list。append(obj)</p></blockquote><h4 id="2-将序列打碎插入"><a href="#2-将序列打碎插入" class="headerlink" title="2.将序列打碎插入"></a>2.将序列打碎插入</h4><blockquote><p>list1.extend(序列)</p></blockquote><h4 id="3-删除指定下标处的元素"><a href="#3-删除指定下标处的元素" class="headerlink" title="3.删除指定下标处的元素"></a>3.删除指定下标处的元素</h4><blockquote><p>list.pop(index)</p></blockquote><h4 id="4-指定下标插入指定的元素"><a href="#4-指定下标插入指定的元素" class="headerlink" title="4.指定下标插入指定的元素"></a>4.指定下标插入指定的元素</h4><blockquote><p>list1.insert(index,obj)</p></blockquote><h4 id="5-函数第一次匹配到的元素"><a href="#5-函数第一次匹配到的元素" class="headerlink" title="5.函数第一次匹配到的元素"></a>5.函数第一次匹配到的元素</h4><blockquote><p>list1.remove(obj)</p></blockquote><h4 id="6-清空列表"><a href="#6-清空列表" class="headerlink" title="6.清空列表"></a>6.清空列表</h4><blockquote><p>list.clear</p></blockquote><ul><li><input disabled type="checkbox"> 这里是引用</li></ul><h4 id="7-删除列表"><a href="#7-删除列表" class="headerlink" title="7.删除列表"></a>7.删除列表</h4><blockquote><p>del list</p></blockquote><h4 id="8-统计指定元素出现的次数"><a href="#8-统计指定元素出现的次数" class="headerlink" title="8.统计指定元素出现的次数"></a>8.统计指定元素出现的次数</h4><blockquote><p>list2.count(obj)</p></blockquote><h4 id="9-返回列表中最大-最小值"><a href="#9-返回列表中最大-最小值" class="headerlink" title="9.返回列表中最大/最小值"></a>9.返回列表中最大/最小值</h4><blockquote><p>max(list1) / min(list1)</p></blockquote><h4 id="10-倒序列表"><a href="#10-倒序列表" class="headerlink" title="10.倒序列表"></a>10.倒序列表</h4><blockquote><p>list1.reverse()</p></blockquote><h4 id="11-将列表排序"><a href="#11-将列表排序" class="headerlink" title="11.将列表排序"></a>11.将列表排序</h4><blockquote><p>list1.sort()</p></blockquote><p>默认升序 ,reverse =True 降序</p>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>写个atm机</title>
<link href="/2019/07/21/python%E5%86%99%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84atm%E6%9C%BA/"/>
<url>/2019/07/21/python%E5%86%99%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84atm%E6%9C%BA/</url>
<content type="html"><![CDATA[<h1 id="atm类"><a href="#atm类" class="headerlink" title="atm类:"></a>atm类:</h1><p>对象:名字 身份证 电话号码 卡</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span><span class="params">()</span>:</span></span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,name,idcard,phonenum,card)</span>:</span></span><br><span class="line"> self.name = name</span><br><span class="line"> self.idcard = idcard</span><br><span class="line"> self.phonenum = phonenum</span><br><span class="line"> self.card = card</span><br></pre></td></tr></table></figure><h1 id="card类"><a href="#card类" class="headerlink" title="card类"></a>card类</h1><p> 对象: 卡号,密码,钱,锁卡与否<br> <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Card</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,cardnum,password,money,islock=False)</span>:</span></span><br><span class="line"> self.cardnum = cardnum</span><br><span class="line"> self.password = password</span><br><span class="line"> self.money = money</span><br><span class="line"> self.islock = islock</span><br></pre></td></tr></table></figure></p><h1 id="atm类-1"><a href="#atm类-1" class="headerlink" title="atm类"></a>atm类</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#Card 类 和 User 类 所在的位置</span></span><br><span class="line"><span class="keyword">from</span> 作业.ATM.card <span class="keyword">import</span> Card</span><br><span class="line"><span class="keyword">from</span> 作业.ATM.user <span class="keyword">import</span> User</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ATM</span>:</span></span><br><span class="line"></span><br><span class="line"> userDict={}</span><br><span class="line"> <span class="comment">#是否登录</span></span><br><span class="line"> islogin = <span class="literal">None</span></span><br><span class="line"> <span class="comment">#尝试密码的次数</span></span><br><span class="line"> changshicishu = <span class="number">0</span></span><br><span class="line"></span><br><span class="line"><span class="meta"> @staticmethod</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">welcome</span><span class="params">()</span>:</span></span><br><span class="line"> print(<span class="string">'''</span></span><br><span class="line"><span class="string"> **********************</span></span><br><span class="line"><span class="string"> * *</span></span><br><span class="line"><span class="string"> * welcome to bank *</span></span><br><span class="line"><span class="string"> * *</span></span><br><span class="line"><span class="string"> **********************</span></span><br><span class="line"><span class="string"> '''</span>)</span><br><span class="line"><span class="comment">#静态方法 :普通方法和类无关</span></span><br><span class="line"><span class="meta"> @staticmethod</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">select</span><span class="params">()</span>:</span></span><br><span class="line"> print(<span class="string">'''</span></span><br><span class="line"><span class="string"> **********************</span></span><br><span class="line"><span class="string"> * 1.登陆 2.开户 *</span></span><br><span class="line"><span class="string"> * 3.查询 4.取款 *</span></span><br><span class="line"><span class="string"> * 5.存款 0.退出 *</span></span><br><span class="line"><span class="string"> * 6.转账 7.改密 *</span></span><br><span class="line"><span class="string"> * 8.注销 9.解锁 *</span></span><br><span class="line"><span class="string"> **********************</span></span><br><span class="line"><span class="string"> '''</span>)</span><br><span class="line"> num = input(<span class="string">"请选择服务项目:"</span>)</span><br><span class="line"> <span class="keyword">return</span> num</span><br><span class="line"></span><br><span class="line"><span class="meta"> @staticmethod</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">getcardnum</span><span class="params">()</span>:</span></span><br><span class="line"> cardnum = <span class="string">""</span></span><br><span class="line"> <span class="keyword">for</span> x <span class="keyword">in</span> range(<span class="number">6</span>):</span><br><span class="line"> cardnum += str(random.randrange(<span class="number">0</span>,<span class="number">10</span>))</span><br><span class="line"> <span class="keyword">return</span> cardnum</span><br><span class="line"> <span class="comment">#开户 类方法: 给类使用 第一个参数为 cls</span></span><br><span class="line"><span class="meta"> @classmethod </span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">openUser</span><span class="params">(cls)</span>:</span></span><br><span class="line"> name = input(<span class="string">"请输入您的姓名:"</span>)</span><br><span class="line"> idcard = input(<span class="string">"请输入您的身份证号码:"</span>)</span><br><span class="line"> phonenum = input(<span class="string">"请输入您的电话号码:"</span>)</span><br><span class="line"> psd = input(<span class="string">"请设置您的密码:"</span>)</span><br><span class="line"> psd2 = input(<span class="string">"请确认您的密码:"</span>)</span><br><span class="line"> <span class="keyword">if</span> psd == psd2:</span><br><span class="line"> mon = int(input(<span class="string">"请输入您的预存余额:"</span>))</span><br><span class="line"> <span class="keyword">if</span> mon><span class="number">0</span>:</span><br><span class="line"> cardnum = cls.getcardnum()</span><br><span class="line"> card = Card(cardnum,psd,mon)</span><br><span class="line"> user = User(name,idcard,phonenum,card)</span><br><span class="line"> cls.userDict[cardnum] = user</span><br><span class="line"> print(<span class="string">"开卡成功,您的卡号为%s,请牢记..."</span>%cardnum)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"预存余额非法,开卡失败..."</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"两次输入密码不一致,开卡失败..."</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment">#登录</span></span><br><span class="line"><span class="meta"> @classmethod</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">login</span><span class="params">(cls)</span>:</span></span><br><span class="line"></span><br><span class="line"> cardnum = input(<span class="string">"请输入您的卡号:"</span>)</span><br><span class="line"> user = cls.userDict.get(cardnum)</span><br><span class="line"> <span class="keyword">if</span> user:</span><br><span class="line"> <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"> psd = input(<span class="string">"请输入您的密码:"</span>)</span><br><span class="line"> <span class="keyword">if</span> psd == user.card.password:</span><br><span class="line"> print(<span class="string">"恭喜你,登录成功!!!"</span>)</span><br><span class="line"> cls.changshicishu = <span class="number">0</span></span><br><span class="line"> cls.islogin = cardnum</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> cls.changshicishu += <span class="number">1</span></span><br><span class="line"> 如果尝试次数大于三 将锁卡</span><br><span class="line"> <span class="keyword">if</span> cls.changshicishu >= <span class="number">3</span>:</span><br><span class="line"> print(<span class="string">"卡已被锁,请先解锁"</span>)</span><br><span class="line"> cls.islogin = <span class="literal">False</span></span><br><span class="line"> print(cls.islogin)</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"密码错误,请重新输入,还有%d次将锁卡"</span>%(<span class="number">3</span>-cls.changshicishu))</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"卡号不存在,请查证后登录。。。"</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 查询</span></span><br><span class="line"><span class="meta"> @classmethod</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">search</span><span class="params">(cls)</span>:</span></span><br><span class="line"> print(cls.islogin)</span><br><span class="line"> <span class="keyword">if</span> cls.islogin:</span><br><span class="line"> print(<span class="string">"您当前的余额为%d"</span>%(cls.userDict.get(cls.islogin).card.money))</span><br><span class="line"></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"请先登录"</span>)</span><br><span class="line"> <span class="comment">#取钱</span></span><br><span class="line"><span class="meta"> @classmethod</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">setmoney</span><span class="params">(cls)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> cls.islogin:</span><br><span class="line"> money = int(input(<span class="string">"请输入您要取的金额:"</span>))</span><br><span class="line"> print(cls.userDict.get(cls.islogin).card.money)</span><br><span class="line"> <span class="keyword">if</span> cls.islogin:</span><br><span class="line"> <span class="keyword">if</span> cls.userDict.get(cls.islogin).card.money - money >= <span class="number">0</span> :</span><br><span class="line"> print(<span class="string">"取款%d成功!"</span>%money)</span><br><span class="line"> cls.userDict.get(cls.islogin).card.money -= money</span><br><span class="line"> print(<span class="string">"您卡里的余额为%d"</span>%cls.userDict.get(cls.islogin).card.money)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"></span><br><span class="line"> print(<span class="string">"余额不足,取款失败!"</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"未登录,请登录后再试"</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"请先登录"</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment">#存钱</span></span><br><span class="line"><span class="meta"> @classmethod</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">getmoney</span><span class="params">(cls)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> cls.islogin:</span><br><span class="line"> <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"> gmoney = int(input(<span class="string">"请输入您要存储的款项:"</span>))</span><br><span class="line"> <span class="keyword">if</span> gmoney > <span class="number">0</span> :</span><br><span class="line"> cls.userDict.get(cls.islogin).card.money += gmoney</span><br><span class="line"> print(<span class="string">"您卡上的余额为%d"</span>%cls.userDict.get(cls.islogin).card.money)</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"输入有误,请重新输入金额"</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"请先登录!"</span>)</span><br><span class="line"> <span class="comment">#转账</span></span><br><span class="line"><span class="meta"> @classmethod</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">zhuanzhang</span><span class="params">(cls)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> cls.islogin:</span><br><span class="line"> <span class="keyword">while</span> <span class="number">1</span>:</span><br><span class="line"> kahao = input(<span class="string">"您需要转到的卡号为:"</span>)</span><br><span class="line"> <span class="keyword">for</span> x <span class="keyword">in</span> cls.userDict.keys():</span><br><span class="line"> <span class="keyword">if</span> x == kahao :</span><br><span class="line"> print(<span class="string">"您要转的卡号为%s,持卡人为%s"</span>%(x,cls.userDict[x].name))</span><br><span class="line"> amoney = int(input(<span class="string">"您需要转的金额为"</span>))</span><br><span class="line"> <span class="comment">#转账人卡内余额减少</span></span><br><span class="line"> cls.userDict.get(cls.islogin).card.money -= amoney</span><br><span class="line"> <span class="comment">#被转账人卡内余额增加</span></span><br><span class="line"> cls.userDict.get(x).card.money += amoney</span><br><span class="line"></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">continue</span></span><br><span class="line"> print(<span class="string">"卡号有误,请重新输入"</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"请先登录!"</span>)</span><br><span class="line"> <span class="comment">#改密码</span></span><br><span class="line"><span class="meta"> @classmethod</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">gaimi</span><span class="params">(cls)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> cls.islogin:</span><br><span class="line"> mima1 = input(<span class="string">"请输入您需要修改的密码:"</span>)</span><br><span class="line"> mima2 = input(<span class="string">"请再次输入修改的密码:"</span>)</span><br><span class="line"> <span class="keyword">if</span> mima1 == mima2:</span><br><span class="line"> cls.userDict.get(cls.islogin).card.password = mima1</span><br><span class="line"> print(<span class="string">"您的新密码为%s"</span>%mima1)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"请先登录"</span>)</span><br><span class="line"> <span class="comment">#锁卡</span></span><br><span class="line"><span class="meta"> @classmethod</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">suoka</span><span class="params">(cls)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> cls.changshicishu >=<span class="number">3</span>:</span><br><span class="line"> print(<span class="string">"卡以被锁,请先解锁"</span>)</span><br><span class="line"> <span class="comment">#解锁</span></span><br><span class="line"><span class="meta"> @classmethod</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">jiesuo</span><span class="params">(cls)</span>:</span></span><br><span class="line"> print(<span class="string">"请输入您的卡号和密码"</span>)</span><br><span class="line"> kahao1 = input(<span class="string">"请输入卡号:"</span>)</span><br><span class="line"> mima3 = input(<span class="string">"请输入密码:"</span>)</span><br><span class="line"> <span class="keyword">for</span> x <span class="keyword">in</span> cls.userDict.keys():</span><br><span class="line"> <span class="keyword">if</span> kahao1 == x:</span><br><span class="line"> <span class="keyword">if</span> mima3 == cls.userDict.get(x).card.password:</span><br><span class="line"> print(<span class="string">"解锁成功"</span>)</span><br><span class="line"> cls.islogin = cls.userDict.get(x).card.cardnum</span><br><span class="line"></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"账号或者密码不正确,请重试"</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"账号或者密码不正确,请重试"</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment">#注销</span></span><br><span class="line"><span class="meta"> @classmethod</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">zhuxiao</span><span class="params">(cls)</span>:</span></span><br><span class="line"> cls.islogin = <span class="literal">False</span></span><br><span class="line"> print(<span class="string">"注销成功"</span>)</span><br></pre></td></tr></table></figure><h1 id="main类"><a href="#main类" class="headerlink" title="main类"></a>main类</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">#ATM类和 time类 所在位置</span></span><br><span class="line"><span class="keyword">from</span> 作业.ATM.atm <span class="keyword">import</span> ATM</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> ATM.welcome()</span><br><span class="line"> <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"> time.sleep(<span class="number">1</span>)</span><br><span class="line"> print(ATM.userDict)</span><br><span class="line"> num = ATM.select()</span><br><span class="line"> <span class="keyword">if</span> num == <span class="string">"2"</span>:</span><br><span class="line"> print(<span class="string">"开户"</span>)</span><br><span class="line"> ATM.openUser()</span><br><span class="line"> <span class="keyword">elif</span> num == <span class="string">"0"</span>:</span><br><span class="line"> print(<span class="string">"退出"</span>)</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">elif</span> num == <span class="string">"1"</span>:</span><br><span class="line"> print(<span class="string">"登录"</span>)</span><br><span class="line"> ATM.login()</span><br><span class="line"> ATM.suoka()</span><br><span class="line"> <span class="keyword">elif</span> num == <span class="string">"3"</span>:</span><br><span class="line"> print(<span class="string">"查询"</span>)</span><br><span class="line"> ATM.search()</span><br><span class="line"> <span class="keyword">elif</span> num == <span class="string">"4"</span>:</span><br><span class="line"> ATM.setmoney()</span><br><span class="line"> <span class="keyword">elif</span> num == <span class="string">"5"</span>:</span><br><span class="line"> ATM.getmoney()</span><br><span class="line"> <span class="keyword">elif</span> num == <span class="string">"6"</span>:</span><br><span class="line"> ATM.zhuanzhang()</span><br><span class="line"> <span class="keyword">elif</span> num == <span class="string">"7"</span>:</span><br><span class="line"> ATM.gaimi()</span><br><span class="line"> <span class="keyword">elif</span> num == <span class="string">"8"</span>:</span><br><span class="line"> ATM.zhuxiao()</span><br><span class="line"> <span class="keyword">elif</span> num == <span class="string">"9"</span>:</span><br><span class="line"> ATM.jiesuo()</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"选择有误请重新选择..."</span>)</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>字符串的使用</title>
<link href="/2019/07/21/%E5%88%A4%E6%96%AD%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E5%90%88%E6%B3%95/"/>
<url>/2019/07/21/%E5%88%A4%E6%96%AD%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E5%90%88%E6%B3%95/</url>
<content type="html"><![CDATA[<p>从控制输入一串字符串,要求字符串只能数字字母下划线,并且长度大于等于20,<br>若不符合条件重新输入,输入完毕之后,要求从控制台输入一个字符,使用自己输入的字符,<br>来对字符串进行切片,切片完毕之后,并且去掉空串,删除列表中的重复元素。</p><a id="more"></a><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> <span class="number">1</span>:</span><br><span class="line"> str1 = input(<span class="string">"请输入一个字符串:"</span>)</span><br><span class="line"> <span class="keyword">if</span> len(str1) >= <span class="number">20</span>:</span><br><span class="line"> str2 = str1.lower()</span><br><span class="line"> <span class="keyword">for</span> x <span class="keyword">in</span> str2:</span><br><span class="line"> <span class="keyword">if</span> x >= <span class="string">'a'</span> <span class="keyword">and</span> x <= <span class="string">'z'</span> <span class="keyword">or</span> x == <span class="string">"_"</span> <span class="keyword">or</span> x>=<span class="string">"0"</span> <span class="keyword">and</span> x<=<span class="string">"9"</span>:</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"非法字符,请重新输入"</span>)</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"字符串合法"</span>)</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">"长度非法,请重新输入"</span>)</span><br><span class="line"></span><br><span class="line">char = input(<span class="string">"请输入一个字符:"</span>)</span><br><span class="line">print(char)</span><br><span class="line">list1 = str1.split(char)</span><br><span class="line">print(list1)</span><br><span class="line">list2 = []</span><br><span class="line"><span class="keyword">for</span> obj <span class="keyword">in</span> list1:</span><br><span class="line"> <span class="keyword">if</span> obj != <span class="string">""</span> <span class="keyword">and</span> obj <span class="keyword">not</span> <span class="keyword">in</span> list2:</span><br><span class="line"> list2.append(obj)</span><br></pre></td></tr></table></figure><p> 输出结果:<br> <img src="https://img-blog.csdnimg.cn/20190720144533425.png" alt="输出结果"></p>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>字典</title>
<link href="/2019/07/21/%E5%AD%97%E5%85%B8/"/>
<url>/2019/07/21/%E5%AD%97%E5%85%B8/</url>
<content type="html"><![CDATA[<h2 id="字典"><a href="#字典" class="headerlink" title="字典"></a>字典</h2><p>采用键(key)值(value)对的方式存储的<br>本质:是一个无序的集合</p><h3 id="字典中key的特性:"><a href="#字典中key的特性:" class="headerlink" title="字典中key的特性:"></a>字典中key的特性:</h3><p>1.key不能重复<br>key即使重复不会报错,后写的将前写的覆盖<br>2.key必须是不可变的类型<br>若使用可变类型做key,则报错</p><p>不可变类型 number,str,tuple<br>可变类型:list,set,dict</p><h3 id="字典的创建:"><a href="#字典的创建:" class="headerlink" title="字典的创建:"></a>字典的创建:</h3><blockquote><p>dict1 = {key1:value1,key2:value2…}</p></blockquote><p>key与key之间用逗号隔开,key与value使用冒号连接</p><h3 id="字典的访问"><a href="#字典的访问" class="headerlink" title="字典的访问:"></a>字典的访问:</h3><p>字典名[key]<br>若key不存在则报错</p><blockquote><p>字典名.get(key)</p></blockquote><p>功能:通过key访问value,若key不存在则返回None</p><h3 id="添加-更改元素"><a href="#添加-更改元素" class="headerlink" title="添加/更改元素"></a>添加/更改元素</h3><blockquote><p>字典名[key] = value</p></blockquote><p>若keu存在的时候,更改元素,若key不存在则添加元素</p><h3 id="删除字典"><a href="#删除字典" class="headerlink" title="删除字典"></a>删除字典</h3><blockquote><p>字典名.pop(key)</p></blockquote><h3 id="浅拷贝"><a href="#浅拷贝" class="headerlink" title="浅拷贝"></a>浅拷贝</h3><blockquote><p>字典名.copy()</p></blockquote><h3 id="打碎插入"><a href="#打碎插入" class="headerlink" title="打碎插入"></a>打碎插入</h3><p>字典名.update(字典2)</p><h3 id="字典的遍历"><a href="#字典的遍历" class="headerlink" title="字典的遍历"></a>字典的遍历</h3><p>直接使用for循环遍历字典,默认遍历key值</p><blockquote><p>字典名.value()</p></blockquote><p>获取所有的value</p><blockquote><p>字典名.key()</p></blockquote><p>获取所有的key值</p><blockquote><p>字典名.items()</p></blockquote><p>获取所有的key和value</p><h3 id="字典和列表的区别于联系"><a href="#字典和列表的区别于联系" class="headerlink" title="字典和列表的区别于联系"></a>字典和列表的区别于联系</h3><h4 id="联系"><a href="#联系" class="headerlink" title="联系"></a>联系</h4><p>它们都是集合,都是用来存储数据的</p><h4 id="区别"><a href="#区别" class="headerlink" title="区别"></a>区别</h4><p>1.字典以键值对的方式存储,列表只存储了字典中value<br>2.字典是无序的,列表是有序的<br>3.访问字典的时候使用key来进行访问,访问列表的时候使用下标<br>4.字典的查找与插入速度比较快,当数据量较大的时候,list1的查找速度会明显变慢<br>5.字典存储需要占用大量的空间,字典不但要存储value还需要存储key,字典内部的排列是不紧密的<br>list1只需要存储value,并且内部排列是紧密的</p>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>字符串</title>
<link href="/2019/07/21/%E5%AD%97%E7%AC%A6%E4%B8%B2/"/>
<url>/2019/07/21/%E5%AD%97%E7%AC%A6%E4%B8%B2/</url>
<content type="html"><![CDATA[<h1 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h1><p>##字符串的拼接:<br>1.使用”+”号</p><blockquote><p>str3 = str1+str2</p></blockquote><p>2、使用”,”号<br>注意:在输出的时候使用,在逗号的位置会产生一个空格</p><p>3.使用%号<br>%s字符串</p><p>4.使用join函数来进行拼接</p><blockquote><p>str1.join(序列)</p></blockquote><p>功能:以指定的字符串对序列中的元素进行拼接,并且返回拼接后的字符串<br>注意:序列中的元素必须是字符串</p><a id="more"></a><h2 id="字符串的重复"><a href="#字符串的重复" class="headerlink" title="字符串的重复"></a>字符串的重复</h2><blockquote><p>str3 = str1*n</p></blockquote><p>功能:将str1重复n次输出,返回一个新的字符串</p><h2 id="获取字符串中指定的字符:"><a href="#获取字符串中指定的字符:" class="headerlink" title="获取字符串中指定的字符:"></a>获取字符串中指定的字符:</h2><p>通过【索引/下标】来实现的,从0开始进行编号</p><blockquote><p>变量名[索引值]</p></blockquote><p>索引值的取值范围[0,len(str1))<br>在python中我们的索引值可以为负,为负的时候从0开始取。</p><p>##获取字符串的长度</p><blockquote><p>len(str1)</p></blockquote><p>##字符截取</p><blockquote><p>str1[start:stop:step]</p></blockquote><p>start:默认0<br>stop:默认len(str1)<br>step:默认1<br>截取范围[start,stop)</p><blockquote><p>str1[::-1] 字符串的倒叙<br>str1[start::-1] </p></blockquote><p>正着数到start位置,倒着从start位置取>到开始</p><blockquote><p>str1[: end:-1] </p></blockquote><p>倒着取到end的位置,不包含end</p><h2 id="判断是否包含指定的字符串"><a href="#判断是否包含指定的字符串" class="headerlink" title="判断是否包含指定的字符串"></a>判断是否包含指定的字符串</h2><p>str1 in str2<br>若str1是str2的子串则返回True否则返回False</p><blockquote><p>eval(str1)</p></blockquote><p>功能:将字符串转为有效的表达式并且将表达式的结果返回</p><blockquote><p>str(obj)</p></blockquote><p>功能:将其它类型的转为字符串</p><blockquote><p>string.lower()</p></blockquote><p>功能:将大写字母转为小写字母</p><blockquote><p>string.upper()</p></blockquote><p>功能:将小写转为大写字母</p><p>》string.swapcase()</p><p>功能:将大写字母转为小写,将小写字母转为大写</p><blockquote><p>string.title()</p></blockquote><p>功能:将每个单词的首字母大写【标题化的字符串】</p><blockquote><p>string.capitalize()</p></blockquote><p>功能:将开头的首字母大写,其他都小写</p><blockquote><p>string.center(width,fillchar)</p></blockquote><p>功能:返回一个以指定字符串居中长度width使用fillchar来进行填充的字符串</p><blockquote><p>string.ljust(width,fillchar)</p></blockquote><p>功能:返回一个以指定字符串居左长度width使用fillchar来进行填充的字符串</p><blockquote><p>string.rjust(width,fillchar)</p></blockquote><p>功能:返回一个以指定字符串居右长度width使用fillchar来进行填充的字符串</p><blockquote><p>string.zfill(width)</p></blockquote><p>功能:返回一个以指定字符串居右长度width使用0来进行填充的字符串</p><blockquote><p>string.count(x,start,end)</p></blockquote><p>功能:统计x在string中出现的次数,若不指定start与end,默认统计整个字符串<br>若指定start与end则取值范围[start,end)</p><blockquote><p>string.find(sub,start,end)</p></blockquote><p>功能:从左往右在string中查找sub,若找得到则返回第一个匹配的下标值,若找不到<br>则返回-1.<br>若不指定start与end则默认查找整个字符串,若指定start与end则查询范围<br>为[start,end)</p><blockquote><p>string.rfind(sub,start,end)</p></blockquote><p>功能:从右往左在string中查找sub,若找得到则返回第一个匹配的下标值,若找不到<br>则返回-1.<br>若不指定start与end则默认查找整个字符串,若指定start与end则查询范围<br>为[start,end)</p><blockquote><p>string.index(sub,start,end)</p></blockquote><p>功能:从左往右在string中查找sub,若找得到则返回第一个匹配的下标值,若找不到<br>则报错.<br>若不指定start与end则默认查找整个字符串,若指定start与end则查询范围<br>为[start,end)</p><blockquote><p>string.rindex(sub,start,end)</p></blockquote><p>功能:从右往左在string中查找sub,若找得到则返回第一个匹配的下标值,若找不到<br>则报错.<br>若不指定start与end则默认查找整个字符串,若指定start与end则查询范围<br>为[start,end)</p><blockquote><p>string.lstrip(chars)</p></blockquote><p>功能:去掉sting左边指定的chars。若不指定chars,则默认去除空白符<br>空白符:【 \t\r\n\f】</p><blockquote><p>string.rstrip(chars)</p></blockquote><p>功能:去掉sting右边指定的chars。若不指定chars,则默认去除空白符<br>空白符:【 \t\r\n\f】</p><blockquote><p>string.strip(chars)</p></blockquote><p>功能:去掉sting左右两边指定的chars。若不指定chars,则默认去除空白符<br>空白符:【 \t\r\n\f】</p><blockquote><p>string.split(seq,maxsplit)</p></blockquote><p>功能:从左往右以指定的seq对string进行切片,并且将切片后的结果以列表的形式返回。<br>若不指定seq默认情况下使用空白符来进行切片<br>若不指定maxsplit则默认全部切片,若指定则切指定的次数</p><blockquote><p>string.rsplit(seq,maxsplit)</p></blockquote><p>功能:从右往左以指定的seq对string进行切片,并且将切片后的结果以列表的形式返回。<br>若不指定seq默认情况下使用空白符来进行切片<br>若不指定maxsplit则默认全部切片,若指定则切指定的次数</p><blockquote><p>string.splitlines(keepends=True)</p></blockquote><p>功能:对string按行进行切片,切片的结果以列表返回,keepends默认为False<br>不保留换行符,当keepends为True的时候保留换行符</p><blockquote><p>max(str1)</p></blockquote><p>功能:返回str1中ASCII最大的那个</p><blockquote><p>min(str1)</p></blockquote><p>功能:返回str1中ASCII值最小的那个</p><blockquote><p>str1.replace(old,new,count)</p></blockquote><p>参数一:旧字符串<br>参数二:新字符串<br>参数三:替换的次数<br>功能:将str1中指定旧串替换成新串,并且将替换的新的字符串返回。<br>若指定count则替换count次,若不指定,则默认全部替换。</p><p>##字符串的映射替换</p><p>###1.生成一个映射表</p><blockquote><p>table = str3.maketrans(“god”,”123”)</p></blockquote><p>###2.根据映射表进行替换</p><blockquote><p>str1.translate(table)</p></blockquote><blockquote><p>str1.startswith(sub)</p></blockquote><p>功能:判断str1是否以sub开头</p><blockquote><p>str1.endswith(sub)</p></blockquote><p>功能:判断str1是否以sub结束</p><blockquote><p>str1.encode(“utf-8”)</p></blockquote><p>功能:将普通字符串转为二进制字符串<br>utf-8:编码格式<br>注意:编码与解码使用相同的编码格式</p><blockquote><p>bstr.decode(“utf-8”)</p></blockquote><p>功能:将二进制字符串转为普通字符串</p><blockquote><p>str1.isalpha()</p></blockquote><p>功能:判断str1中的字符是否为为字母,若是则返回True,否则返回False<br>注意:中文没有考虑【若出现中文也为True】</p><blockquote><p>str1.isalnum()</p></blockquote><p>功能:判断str1中的字符是否为为字母或者数字,若是则返回True,否则返回False<br>注意:中文没有考虑【若出现中文也为True】</p><blockquote><p>str1.isupper()</p></blockquote><p>功能:判断str1中的所有字符是否全部都大写,若是则返回True,否则返回False</p><blockquote><p>str1.islower()</p></blockquote><p>功能:判断str1中的所有字符是否全部都小写,若是则返回True,否则返回False</p><blockquote><p>str1.istitle()</p></blockquote><p>功能:判断str1是否为标题化的字符串,若是返回True,否则返回False</p><blockquote><p>str1.isspace()</p></blockquote><p>功能:判断str1是否只包含空白符,若是则返回True,否则返回False</p><blockquote><p>ord(char)</p></blockquote><p>功能:返回char的ASCII码值</p><blockquote><p>chr(code)</p></blockquote><p>功能:返回code对应的ASCII的字符</p>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>歌词解析器</title>
<link href="/2019/07/21/%E6%AD%8C%E8%AF%8D%E8%A7%A3%E6%9E%90%E5%99%A8/"/>
<url>/2019/07/21/%E6%AD%8C%E8%AF%8D%E8%A7%A3%E6%9E%90%E5%99%A8/</url>
<content type="html"><![CDATA[<h3 id="python实现一个歌词解析器"><a href="#python实现一个歌词解析器" class="headerlink" title="python实现一个歌词解析器"></a>python实现一个歌词解析器</h3><a id="more"></a><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">getMusicDict</span><span class="params">(musicLrc)</span>:</span></span><br><span class="line"> musicDict = {}</span><br><span class="line"> mList = musicLrc.strip().splitlines()</span><br><span class="line"> <span class="keyword">for</span> line <span class="keyword">in</span> mList:</span><br><span class="line"> lineList = line.split(<span class="string">"]"</span>)</span><br><span class="line"> <span class="keyword">for</span> x <span class="keyword">in</span> lineList[:<span class="number">-1</span>]:</span><br><span class="line"> timelist = x[<span class="number">1</span>:].split(<span class="string">":"</span>)</span><br><span class="line"> <span class="comment">#["00", "12"]</span></span><br><span class="line"> timef = float(timelist[<span class="number">0</span>]) * <span class="number">60</span> + float(timelist[<span class="number">-1</span>])</span><br><span class="line"> musicDict[timef] = lineList[<span class="number">-1</span>]</span><br><span class="line"> <span class="keyword">return</span> musicDict</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">printMusic</span><span class="params">(musicDict)</span>:</span></span><br><span class="line"> tList = list(musicDict)</span><br><span class="line"> tList.sort()</span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> range(len(tList)):</span><br><span class="line"> <span class="keyword">if</span> i == <span class="number">0</span>:</span><br><span class="line"> time.sleep(tList[i])</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> time.sleep(tList[i]-tList[i<span class="number">-1</span>])</span><br><span class="line"> print(musicDict.get(tList[i]))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">musicLrc = <span class="string">'''</span></span><br><span class="line"><span class="string">[00:03.50]传奇</span></span><br><span class="line"><span class="string">[00:19.10]作词:刘兵 作曲:李健</span></span><br><span class="line"><span class="string">[00:20.60]演唱:王菲</span></span><br><span class="line"><span class="string">[00:26.60] </span></span><br><span class="line"><span class="string">[04:40.75][02:39.90][00:36.25]只是因为在人群中多看了你一眼</span></span><br><span class="line"><span class="string">[04:49.00]</span></span><br><span class="line"><span class="string">[02:47.44][00:43.69]再也没能忘掉你容颜</span></span><br><span class="line"><span class="string">[02:54.83][00:51.24]梦想着偶然能有一天再相见</span></span><br><span class="line"><span class="string">[03:02.32][00:58.75]从此我开始孤单思念</span></span><br><span class="line"><span class="string">[03:08.15][01:04.30]</span></span><br><span class="line"><span class="string">[03:09.35][01:05.50]想你时你在天边</span></span><br><span class="line"><span class="string">[03:16.90][01:13.13]想你时你在眼前</span></span><br><span class="line"><span class="string">[03:24.42][01:20.92]想你时你在脑海</span></span><br><span class="line"><span class="string">[03:31.85][01:28.44]想你时你在心田</span></span><br><span class="line"><span class="string">[03:38.67][01:35.05]</span></span><br><span class="line"><span class="string">[04:09.96][03:39.87][01:36.25]宁愿相信我们前世有约</span></span><br><span class="line"><span class="string">[04:16.37][03:46.38][01:42.47]今生的爱情故事 不会再改变</span></span><br><span class="line"><span class="string">[04:24.82][03:54.83][01:51.18]宁愿用这一生等你发现</span></span><br><span class="line"><span class="string">[04:31.38][04:01.40][01:57.43]我一直在你身旁 从未走远</span></span><br><span class="line"><span class="string">[04:39.55][04:09.00][02:07.85]</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line">musicdict = getMusicDict(musicLrc)</span><br><span class="line">printMusic(musicdict)</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>装饰器</title>
<link href="/2019/07/21/%E8%A3%85%E9%A5%B0%E5%99%A8/"/>
<url>/2019/07/21/%E8%A3%85%E9%A5%B0%E5%99%A8/</url>
<content type="html"><![CDATA[<h1 id="装饰器"><a href="#装饰器" class="headerlink" title="装饰器"></a>装饰器</h1><p>在代码运行的期间动态增加功能的方式,我们称之为装饰器。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#最简单的装饰器</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">outer</span><span class="params">(func)</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">inner</span><span class="params">()</span>:</span></span><br><span class="line"> <span class="comment">#增加的功能</span></span><br><span class="line"> func()</span><br><span class="line"> <span class="keyword">return</span> inner</span><br></pre></td></tr></table></figure><h4 id="外函数:"><a href="#外函数:" class="headerlink" title="外函数:"></a>外函数:</h4><p>1.接收被装饰的函数,func :被装饰的函数,<br>2.将装饰好的替代版的函数返回</p><a id="more"></a><h4 id="内函数:"><a href="#内函数:" class="headerlink" title="内函数:"></a>内函数:</h4><p>1.添加动态增加的功能<br>2.执行被装饰的函数</p><p>在内函数中,若被装饰的函数有返回值的时候,我们必须使用return,把被装饰函数的值返回</p><p>若被装饰的函数没有返回值,我们无需使用return。</p><h2 id="比较复杂的装饰器"><a href="#比较复杂的装饰器" class="headerlink" title="比较复杂的装饰器"></a>比较复杂的装饰器</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">outer</span><span class="params">(func)</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">inner</span><span class="params">(参数列表)</span>:</span></span><br><span class="line"> <span class="comment">#添加动态添加的功能</span></span><br><span class="line"> <span class="keyword">return</span> func(参数列表)</span><br><span class="line"> <span class="keyword">return</span> inner</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>常见模块</title>
<link href="/2019/07/21/python%E5%B8%B8%E8%A7%81%E7%9A%84%E6%A8%A1%E5%9D%97/"/>
<url>/2019/07/21/python%E5%B8%B8%E8%A7%81%E7%9A%84%E6%A8%A1%E5%9D%97/</url>
<content type="html"><![CDATA[<h1 id="math模块"><a href="#math模块" class="headerlink" title="math模块"></a>math模块</h1><blockquote><p>math.ceil(x)</p></blockquote><p>功能:对x进行向上求整</p><blockquote><p>math.floor(x)</p></blockquote><p>功能:对x进行向下取整</p><blockquote><p>math.modf(x)</p></blockquote><p>功能:返回x的小数与整数部分以元组的形式返回。</p><blockquote><p>math.sqrt(x)</p></blockquote><p>功能:返回x的开平方根【返回的正数的那个】</p><h1 id="os"><a href="#os" class="headerlink" title="os"></a>os</h1><blockquote><p>os.getcwd()</p></blockquote><p>功能:获取当前目录的绝对路径</p><p>绝对路径:<br>window:以盘符开头,或者以\开头<br>linux:以/开头或者以~开头<br>相对路径:<br>window:以文件开头,以./开头 或者../开头<br>linux:与window相同</p><blockquote><p>os.listdir(path)</p></blockquote><p>功能:显示指定路径下所有的文件名以及目录名,若path不指定,默认当前目录下。</p><blockquote><p>os.path.abspath(path)</p></blockquote><p>功能:将指定的路径拼接到当前所在所在的路径下</p><blockquote><p>os.path.split(path)</p></blockquote><p>功能:返回指定路径目录部分以及文件部分<br>本质:以最后一个/来进行切分,返回两个值,前面的就是目录部分,后面的就是文件部分<br>若是”.”切分到文件部分去</p><blockquote><p>os.path.dirname(path)</p></blockquote><p>功能:返回指定路径的目录部分</p><blockquote><p>os.path.basename(path)</p></blockquote><p>功能:返回指定路径的文件部分</p><blockquote><p>os.path.join(path,paths)</p></blockquote><p>功能:对路径进行拼接处理<br>paths:可以接收多个路径<br>若paths存在绝对路径,前面路径则不保留。</p><blockquote><p>os.path.getsize(path)</p></blockquote><p>功能:获取指定路径文件的大小【目录的大小获取不到】</p><blockquote><p>os.path.exists(path)</p></blockquote><p>功能:判断指定的路径是否存在,若存在则返回True,否则返回False</p><blockquote><p>os.path.isdir(path)</p></blockquote><p>功能:判断该路径是否为目录,若是返回True,否则返回False</p><blockquote><p>os.path.isfile(path)</p></blockquote><p>功能:判断指定的路径是否为文件,若是则返回True,否则返回False</p><blockquote><p>os.remove(path)</p></blockquote><p>功能:删除指定的路径的文件</p><blockquote><p>os.mkdir(path)</p></blockquote><p>功能:创建指定的目录</p><blockquote><p>os.makedirs(path)</p></blockquote><p>功能:递归创建多个目录</p><blockquote><p>os.rmdir(path)</p></blockquote><p>功能:删除指定的空目录</p><blockquote><p>os.removedirs(path)</p></blockquote><p>功能:递归删除目录</p><blockquote><p>os.chdir(path)</p></blockquote><p>功能:切换目录</p><h1 id="time模块"><a href="#time模块" class="headerlink" title="time模块"></a>time模块</h1><blockquote><p>time.time():</p></blockquote><p>功能:获取时间戳</p><blockquote><p>time.gmtime(sec)</p></blockquote><p>功能:将时间戳转为时间元组utc时间</p><blockquote><p>time.localtime(sec)</p></blockquote><p>功能:将时间戳转为时间元组当地时间</p><blockquote><p>time.ctime(sec)</p></blockquote><p>功能:将指定的时间戳转为时间字符串,若时间戳没给,则默认为当前时间</p><blockquote><p>time.asctime(tuple)</p></blockquote><p>功能:将时间元组转为时间字符串</p><blockquote><p>time.strftime(format,t)</p></blockquote><p>功能:将时间元组格式化成指定的时间字符串</p><blockquote><p>time.strptime(strt,format)</p></blockquote><p>功能:将时间字符串使用指定的格式转为时间元组</p><blockquote><p>time.mktime(strp)</p></blockquote><p>功能:将时间元组转为时间戳</p><blockquote><p>time.sleep(sec)</p></blockquote><p>功能:休眠</p><blockquote><p>time.clock()</p></blockquote><p>功能:以浮点数的形式计算的秒数返回当前的cpu执行的时间</p><h1 id="datetime模块"><a href="#datetime模块" class="headerlink" title="datetime模块"></a>datetime模块</h1><p>引用:from datetime import datetime</p><blockquote><p>datetime.now()</p></blockquote><p>功能:获取当前时间</p><blockquote><p>datetime(year,mon,day,hour,min,sec)</p></blockquote><p>功能:返回指定的时间</p><blockquote><p>dt.strftime(format)</p></blockquote><p>功能:将datetime对象转为指定的时间格式<br>timedelta = datetime1 - datetime2<br>功能:两个datetime相减得到一个时间间隔对象。<br>通过时间间隔对象可以获取间隔的天数.days<br>以及除间隔天数之外剩余的秒数 .seconds</p><h1 id="calendar模块"><a href="#calendar模块" class="headerlink" title="calendar模块"></a>calendar模块</h1><blockquote><p>calendar.month(year,mon)–</p></blockquote><p>功能:返回月日历</p><blockquote><p>calendar.calendar(year)</p></blockquote><p>功能:返回年日历</p><blockquote><p>calendar.isleap(year)</p></blockquote><p>功能:判断某年是否为闰年,闰年返回True,平年返回False</p><blockquote><p>calendar.leapdays(year1,year2)</p></blockquote><p>功能:返回[year1,year2)之间闰年的个数</p><blockquote><p>calendar.monthrange(year,mon)</p></blockquote><p>功能:返回本月第一天的星期码与本月的天数</p><blockquote><p>calendar.monthcalendar(year,mon)</p></blockquote><p>返回以每周每天为元素的序列</p><blockquote><p>calendar.weekday(year,mon,day)</p></blockquote><p>功能:返回指定日期的星期码</p><h1 id="json模块"><a href="#json模块" class="headerlink" title="json模块"></a>json模块</h1><p>功能:只能序列化python标准模块</p><p>json可以直接序列化python基本数据类型。</p><blockquote><p>json.dumps(obj,default)</p></blockquote><p>注意:若obj为python基本数据类型,我们无需写default,直接进行序列化<br>若obj为自定义的数据类型,这时候default后面跟的是将对象转为基本数据类型的函数<br>功能:将obj使用default函数转为字典并且序列化为json字符串,并且返回</p><blockquote><p>json.dump(obj,f,default)</p></blockquote><p>功能:将obj使用default函数转为字典并且序列化为json字符串,<br>并且写入到指定的打开的f中。</p><blockquote><p>json.loads(jsonstr,object_hook)</p></blockquote><p>功能:将jsonstr转为字典,通过object_hook的方法将字典转为对象</p><blockquote><p>json.load(f,object_hook)</p></blockquote><p>功能:读取打开文件的内容,并且将读取的内容转为字典,<br>通过object_hook的方法将字典转为对象。</p>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>面向对象</title>
<link href="/2019/07/21/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/"/>
<url>/2019/07/21/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/</url>
<content type="html"><![CDATA[<h1 id="面向对象"><a href="#面向对象" class="headerlink" title="面向对象"></a>面向对象</h1><p>面向对象:基于万物皆对象的哲学思想<br>面向对象只是一种思想,并不是语言。<br>面向对象和面向过程的区别:<br>1.都是看待问题的方式,都能解决问题<br>2.面向对象着眼于找到一个具有特殊功能的对象,然后委托给这个对象给你完成问题。<br>3.面向过程在于问题的细节,所有事情亲力亲为<br>python是面向对象的程序设计语言,类与对象是面向对象的核心</p><h2 id="类:"><a href="#类:" class="headerlink" title="类:"></a>类:</h2><p>一个具有特殊功能实体的集合<br>类的设计:<br>1.事物名称[类名] Person类<br>2.事物的特征:[名词][属性]name sex age height weight<br>3.事物的行为:[动词][方法]eat sleep playgame coding shopping</p><p>实例化对象:</p><blockquote><p>对象名 = 类名()</p></blockquote><h2 id="对象:"><a href="#对象:" class="headerlink" title="对象:"></a>对象:</h2><p>在类中,一个具有特殊功能的实体,对象才能解决特定的问题,对象通常情况下<br>也称实例。</p><p>类与对象的关系:<br>类是用于描述某一对象的共同特征[类是对象的抽象]<br>对象是类的具有体现。</p><p>注意:类也是一种数据类型,只不过这种数据类型是你自己定义的,类本身并不占用空间<br>对象是占用空间的。</p><h2 id="构造函数"><a href="#构造函数" class="headerlink" title="构造函数"></a>构造函数</h2><p>__ init __ 在实例化对象/创建对象的时候调用</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">python</span><br><span class="line"> def__init__(self,name,sex,address,age,facevalue,hobby):</span><br><span class="line"> # print("构造函数被调用啦。。")</span><br><span class="line"> self.name = name</span><br><span class="line"> self.sex = sex</span><br><span class="line"> self.address = address</span><br><span class="line"> self.age = age</span><br><span class="line"> self.facevalue = facevalue</span><br><span class="line"> self.hobby = hobby</span><br><span class="line"></span><br><span class="line"> def ins(self):</span><br><span class="line"> print("你好,我是你同桌%s,我今年%d岁,我是一个%s,颜值%d,我喜欢%s,我来自于%s"</span><br><span class="line"> %(self.name,self.age,self.sex,self.facevalue,self.hobby,self.address))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">if __name__ == '__main__':</span><br><span class="line"> mate = ClassMate("lili","girl","广东",18,90,"吃")</span><br><span class="line"> print(mate.name)</span><br><span class="line"> mate.ins()</span><br></pre></td></tr></table></figure><h2 id="析构函数"><a href="#析构函数" class="headerlink" title="析构函数"></a>析构函数</h2><p>__ del __ :在对象被销毁的时候调用</p><h2 id="self-的使用"><a href="#self-的使用" class="headerlink" title="self 的使用"></a>self 的使用</h2><p>1.self不是一个关键字,可以使用其他的变量名,但是建议使用self,这是一种约定俗成的一种写法<br>2.self的位置是固定的,必须写在参数列表的第一个位置<br>3.self代表对象自己本身[类的实例]<br>4.该参数不需要我们手动传递,当使用对象调用此函数的时候,对象会将自己本身自动的传递进去</p><h2 id="成员变量和类变量"><a href="#成员变量和类变量" class="headerlink" title="成员变量和类变量"></a>成员变量和类变量</h2><h3 id="类变量:"><a href="#类变量:" class="headerlink" title="类变量:"></a>类变量:</h3><p>[又称静态成员变量]<br>1.定义在类中,并且定义在函数体外<br>2.调用类变量的时候,使用类名来进行调用,若使用对象来进行调用它也不报错[不建议这么使用]<br>3.调用的优先级,当使用对象来进行调用的时候,优先寻找成员变量,只有成员变了不存在的时候才找<br>类变量</p><h3 id="成员变量:"><a href="#成员变量:" class="headerlink" title="成员变量:"></a>成员变量:</h3><p>1.定义类的函数中,并且绑定在self的身上<br>2.调用成员变量的时候,使用对象来进行调用,不能使用类名来进行调用</p><p>注意:使用对象来更改类变量的时候,更改的结果只作用于当前对象,若使用类名来进行更改<br>更改结果作用于对于其更改之后的所有的对象。</p><h2 id="成员方法和静态方法和类方法"><a href="#成员方法和静态方法和类方法" class="headerlink" title="成员方法和静态方法和类方法"></a>成员方法和静态方法和类方法</h2><h3 id="成员方法:"><a href="#成员方法:" class="headerlink" title="成员方法:"></a>成员方法:</h3><p>通常情况下在类中的方法,都是成员方法<br>成员的方法的特征,第一个参数[self],代表对象本身,<br>调用成员方法的时候必须使用对象来进行调用。</p><h3 id="静态方法:"><a href="#静态方法:" class="headerlink" title="静态方法:"></a>静态方法:</h3><p>就是一普通方法,但是写到了类中,<br>在此方法中与对象与类都没有关系。此时,我们可以将此方法定义为静态方法。<br>静态方法我们使用@staticmethod装饰器来进行标识。<br>调用的时候我们使用类名来进行调用。[若使用对象来进行调用也不报错]</p><h3 id="类方法:"><a href="#类方法:" class="headerlink" title="类方法:"></a>类方法:</h3><p>类方法是绑定在类身上的一类方法,专门给类使用的。类在使用<br>此方法的时候,会将自身作为参数,传递给参数列表中第一个参数。<br>创建类方法的时候,使用@classmethod进行标识。</p><h3 id="三种方法的使用时机:"><a href="#三种方法的使用时机:" class="headerlink" title="三种方法的使用时机:"></a>三种方法的使用时机:</h3><p>1.在当前方法中,若有使用到类变量/类方法/静态方法,但是没有成员变量/成员方法,<br>这时候我可以将其写成类方法</p><p>2.当在当前函数中,使用到了成员变量/成员方法,这时候就必须将此方法写成成员方法</p><p>3.在当前函数中,没有使用类变量/类方法/静态方法/成员方法/成员变量的时候,<br>我们就可以将此函数写为静态方法。</p><h3 id="总结:"><a href="#总结:" class="headerlink" title="总结:"></a>总结:</h3><p>1.成员方法不需要装饰器,类方法需要@classmethod,静态方法需要@staticmethod<br>2.成员方法与类方法都有一个隐式参数,成员方法的是self[代表对象本身],类方法的是cls[代表类本身]<br>定义的时候需要将其声明在参数列表的第一个位置。<br>3.成员方法使用对象来进行调用,静态方法与类方法使用类名来进行调用,[<br>但是若使用对象来进行调用也不报错]</p><h2 id="访问限制"><a href="#访问限制" class="headerlink" title="访问限制"></a>访问限制</h2><h3 id="面向对象语言三大特征:"><a href="#面向对象语言三大特征:" class="headerlink" title="面向对象语言三大特征:"></a>面向对象语言三大特征:</h3><p> 封装、继承、多态</p><h4 id="封装:"><a href="#封装:" class="headerlink" title="封装:"></a>封装:</h4><p>广义的封装:类与函数的定义的本身就是封装的体现<br>狭义的封装:类中某些属性,我们不希望外界能够直接访问,这时候,我们<br>可以将此属性私有化只有本类持有,然后给外界暴露出一个访问接口即可。<br>封装的本质:属性私有化的过程</p><h5 id="优点:"><a href="#优点:" class="headerlink" title="优点:"></a>优点:</h5><p>1.提高复用性<br>2.保证数据的安全性</p><p>@property装饰器可以将函数作为属性来进行调用,与此同时他还可以生成一个新的<br>装饰器,新的装饰器的名字就是@属性.setter, 新的装饰器用来装饰setter函数。</p><p>优点:方便调用</p><h4 id="继承:"><a href="#继承:" class="headerlink" title="继承:"></a>继承:</h4><p>若两个或者两个以上的类具有相同的属性以及方法,我们可以抽出一个类。<br>在抽取出来的这个类中写共通的部分,那么这个被抽取出来的类我们称之为<br>父类[超类/基类],其他类我们称之为子类或者派生类。</p><p>class 类名(object):<br> pass<br>():继承列表,当小括号没有内容或者小括号不写,或许写object的时候,意思是一样的。<br>默认继承是object,换句话说:object类是一切类的基类。<br>单继承:一个子类继承一个父类我们称之为单继承</p><h5 id="总结:-1"><a href="#总结:-1" class="headerlink" title="总结:"></a>总结:</h5><p>1.子类可以直接继承父类所有未私有属性以及方法<br>2.父类不能调用子类中特有的属性以及方法<br>3.当子类中含有特殊的属性的时候,需要在子类中重写init函数,<br>并且在此函数将所有的属性进行声明。并且需要手动调用父类中的init的方法</p><h5 id="优点:-1"><a href="#优点:-1" class="headerlink" title="优点:"></a>优点:</h5><p>1.提高代码的复用性<br>2.提高代码的可维护性<br>3.提高代码的安全性</p><h5 id="缺点:"><a href="#缺点:" class="headerlink" title="缺点:"></a>缺点:</h5><p>增加代码耦合性。</p><h5 id="多继承"><a href="#多继承" class="headerlink" title="多继承"></a>多继承</h5><p>一个子类继承多个父类,我们称之为多继承。<br>比如:一个孩子,有一个爸爸,有一个妈妈</p><h6 id="总结:-2"><a href="#总结:-2" class="headerlink" title="总结:"></a>总结:</h6><p>1.子类可以继承所有父类未私有化的属性以及方法<br>2.若子类存在的特有的/当继承父类中的属性不一致的时候,<br>我们手动调用父类中的init,父类名.<strong>init</strong>(self, 参数列表)<br>3.当多个父类出现同名的函数的时候,子类优先继承写在参数列表前面的那个</p><h4 id="多态"><a href="#多态" class="headerlink" title="多态"></a>多态</h4><h5 id="在Python中"><a href="#在Python中" class="headerlink" title="在Python中"></a>在Python中</h5><p>鸭子模型:若有一只鸟,走路像鸭子,叫声像鸭子,我们可以称这只鸟就是鸭子。<br>在python中我们不关心数据的类型,我们只关心数据的属性以及其方法的使用。</p><h5 id="真正的多态:"><a href="#真正的多态:" class="headerlink" title="真正的多态:"></a>真正的多态:</h5><p>指一个事物的多种表现形态,必须依赖于继承而存在。<br>isinstance(obj,types)<br>功能:判断obj是否属于指定的types,或者types的子类<br>types:也可以是一个元组。若是元组,则判断的是obj是否属于types元组中<br>的某种类型。<br>若有一个成立,得到的结果就为True,否则为False。</p><p>dir(obj)<br>功能:可以查看obj所有的属性以及方法以列表的形式返回。</p><h4 id="函数重写"><a href="#函数重写" class="headerlink" title="函数重写"></a>函数重写</h4><p><strong>str</strong>系统提供的函数<br>当调用str/print的时候会自动调用此函数,此函数是给用户使用的,<br>用来描述对象的显示。</p><p>注意:<strong>str</strong>函数的返回值必须是一个字符串,并且当调用print的时候<br>打印对象的结果就是此函数的返回值</p><p>若在类中存在<strong>repr</strong>,但是不存在<strong>str</strong>,<br>我们可以使用 <strong>str</strong> = <strong>repr</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span>:</span></span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,name,age)</span>:</span></span><br><span class="line"> self.name = name</span><br><span class="line"> self.age = age</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__repr__</span><span class="params">(self)</span>:</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"%s-%d"</span>%(self.name,self.age)</span><br><span class="line"></span><br><span class="line"> __str__ = __repr__</span><br><span class="line"><span class="comment">#重写了add函数</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__add__</span><span class="params">(self, other)</span>:</span></span><br><span class="line"> <span class="keyword">return</span> Person(self.name+other.name,self.age+other.age)</span><br></pre></td></tr></table></figure><h4 id="运算重载"><a href="#运算重载" class="headerlink" title="运算重载"></a>运算重载</h4><p> <strong>repr</strong></p><h4 id="单例模式"><a href="#单例模式" class="headerlink" title="单例模式"></a>单例模式</h4><p>写在<strong>init</strong>前面的函数<br>__ new__</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__new__</span><span class="params">(cls, *args, **kwargs)</span>:</span></span><br><span class="line"> print(<span class="string">"new方法被调用了"</span>)</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,name)</span>:</span></span><br><span class="line"> self.name = name</span><br><span class="line"> print(<span class="string">"init方法被调用了"</span>)</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> python基础 </tag>
</tags>
</entry>
<entry>
<title>M L。</title>
<link href="/2019/07/21/%E7%AC%AC%E4%B8%80%E9%97%A8%E8%AF%BE%EF%BC%9A%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%E5%92%8C%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/"/>
<url>/2019/07/21/%E7%AC%AC%E4%B8%80%E9%97%A8%E8%AF%BE%EF%BC%9A%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%E5%92%8C%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/</url>
<content type="html"><![CDATA[<h1 id="第一周:深度学习引言-Introduction-to-Deep-Learning"><a href="#第一周:深度学习引言-Introduction-to-Deep-Learning" class="headerlink" title="第一周:深度学习引言(Introduction to Deep Learning)"></a>第一周:深度学习引言(Introduction to Deep Learning)</h1><h2 id="1-欢迎-Welcome"><a href="#1-欢迎-Welcome" class="headerlink" title=".1 欢迎(Welcome)"></a>.1 欢迎(Welcome)</h2><p>第一个视频主要讲了什么是深度学习,深度学习能做些什么事情。以下是吴恩达老师的原话:</p><p>深度学习改变了传统互联网业务,例如如网络搜索和广告。但是深度学习同时也使得许多新产品和企业以很多方式帮助人们,从获得更好的健康关注。</p><p>深度学习做的非常好的一个方面就是读取X光图像,到生活中的个性化教育,到精准化农业,甚至到驾驶汽车以及其它一些方面。如果你想要学习深度学习的这些工具,并应用它们来做这些令人窒息的操作,本课程将帮助你做到这一点。当你完成cousera上面的这一系列专项课程,你将能更加自信的继续深度学习之路。在接下来的十年中,我认为我们所有人都有机会创造一个惊人的世界和社会,这就是AI(人工智能)的力量。我希望你们能在创建AI(人工智能)社会的过程中发挥重要作用。</p><p>我认为AI是最新的电力,大约在一百年前,我们社会的电气化改变了每个主要行业,从交通运输行业到制造业、医疗保健、通讯等方面,我认为如今我们见到了AI明显的令人惊讶的能量,带来了同样巨大的转变。显然,AI的各个分支中,发展的最为迅速的就是深度学习。因此现在,深度学习是在科技世界中广受欢迎的一种技巧。</p><p>通过这个课程,以及这门课程后面的几门课程,你将获取并且掌握那些技能。</p><p>下面是你将学习到的内容:</p><p>在cousera的这一系列也叫做专项课程中,在第一门课中(神经网络和深度学习),你将学习神经网络的基础,你将学习神经网络和深度学习,这门课将持续四周,专项课程中的每门课将持续2至4周。</p><p>但是在第一门课程中,你将学习如何建立神经网络(包含一个深度神经网络),以及如何在数据上面训练他们。在这门课程的结尾,你将用一个深度神经网络进行辨认猫。<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy9hYzQ3N2JhNjBlMTIwZTMxOGNmYjJlZDcxZmY1NTQ3OC5wbmc" alt="小猫"><br>由于某种原因,第一门课会以猫作为对象识别。</p><p>接下来在第二门课中,我们将使用三周时间。你将进行深度学习方面的实践,学习严密地构建神经网络,如何真正让它表现良好,因此你将要学习超参数调整、正则化、诊断偏差和方差以及一些高级优化算法,比如Momentum和Adam算法,犹如黑魔法一样根据你建立网络的方式。第二门课只有三周学习时间。</p><p>在第三门课中,我们将使用两周时间来学习如何结构化你的机器学习工程。事实证明,构建机器学习系统的策略改变了深度学习的错误。</p><p>举个例子:你分割数据的方式,分割成训练集、比较集或改变的验证集,以及测试集合,改变了深度学习的错误。</p><p>所以最好的实践方式是什么呢?</p><p>你的训练集和测试集来自不同的贡献度在深度学习中的影响很大,那么你应该怎么处理呢?</p><p>如果你听说过端对端深度学习,你也会在第三门课中了解到更多,进而了解到你是否需要使用它,第三课的资料是相对比较独特的,我将和你分享。我们了解到的所有的热门领域的建立并且改良许多的深度学习问题。这些当今热门的资料,绝大部分大学在他们的深度学习课堂上面里面不会教的,我认为它会提供你帮助,让深度学习系统工作的更好。</p><p>在第四门课程中,我们将会提到卷积神经网络(CNN(s)),它经常被用于图像领域,你将会在第四门课程中学到如何搭建这样的模型。</p><p>最后在第五门课中,你将会学习到序列模型,以及如何将它们应用于自然语言处理,以及其它问题。</p><p>序列模型包括的模型有循环神经网络(RNN)、全称是长短期记忆网络(LSTM)。你将在课程五中了解其中的时期是什么含义,并且有能力应用到自然语言处理(NLP)问题。</p><p>总之你将在课程五中学习这些模型,以及能够将它们应用于序列数据。比如说,自然语言就是一个单词序列。你也将能够理解这些模型如何应用到语音识别或者是编曲以及其它问题。</p><p>因此,通过这些课程,你将学习深度学习的这些工具,你将能够去使用它们去做一些神奇的事情,并借此来提升你的职业生涯。</p><p>吴恩达</p><h2 id="1-2-什么是神经网络?-What-is-a-Neural-Network"><a href="#1-2-什么是神经网络?-What-is-a-Neural-Network" class="headerlink" title="1.2 什么是神经网络?(What is a Neural Network)"></a>1.2 什么是神经网络?(What is a Neural Network)</h2><p>我们常常用深度学习这个术语来指训练神经网络的过程。有时它指的是特别大规模的神经网络训练。那么神经网络究竟是什么呢?在这个视频中,我会讲解一些直观的基础知识。</p><p>让我们从一个房价预测的例子开始讲起。</p><p>假设你有一个数据集,它包含了六栋房子的信息。所以,你知道房屋的面积是多少平方英尺或者平方米,并且知道房屋价格。这时,你想要拟合一个根据房屋面积预测房价的函数。</p><p>如果你对线性回归很熟悉,你可能会说:“好吧,让我们用这些数据拟合一条直线。”于是你可能会得到这样一条直线。</p><p><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy8zZmU2ZGEyNjAxNDQ2NzI0M2UzZDQ5OTU2OWJlMzY3NS5wbmc" alt><br>但有点奇怪的是,你可能也发现了,我们知道价格永远不会是负数的。因此,为了替代一条可能会让价格为负的直线,我们把直线弯曲一点,让它最终在零结束。这条粗的蓝线最终就是你的函数,用于根据房屋面积预测价格。有部分是零,而直线的部分拟合的很好。你也许认为这个函数只拟合房屋价格。</p><p>作为一个神经网络,这几乎可能是最简单的神经网络。我们把房屋的面积作为神经网络的输入(我们称之为),通过一个节点(一个小圆圈),最终输出了价格(我们用y表示)。其实这个小圆圈就是一个单独的神经元。接着你的网络实现了左边这个函数的功能。</p><p>在有关神经网络的文献中,你经常看得到这个函数。从趋近于零开始,然后变成一条直线。这个函数被称作ReLU激活函数,它的全称是Rectified Linear Unit。rectify(修正)可以理解成max(0,x),这也是你得到一个这种形状的函数的原因。<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy85NDMzZDYzNmVlZWFhMzQ2OTkxMGFlNzUyOTg4MjBjYy5wbmc" alt="在这里插入图片描述"><br>你现在不用担心不理解ReLU函数,你将会在这门课的后面再次看到它。</p><p>如果这是一个单神经元网络,不管规模大小,它正是通过把这些单个神经元叠加在一起来形成。如果你把这些神经元想象成单独的乐高积木,你就通过搭积木来完成一个更大的神经网络。</p><p>让我们来看一个例子,我们不仅仅用房屋的面积来预测它的价格,现在你有了一些有关房屋的其它特征,比如卧室的数量,或许有一个很重要的因素,一家人的数量也会影响房屋价格,这个房屋能住下一家人或者是四五个人的家庭吗?而这确实是基于房屋大小,以及真正决定一栋房子是否能适合你们家庭人数的卧室数。</p><p>换个话题,你可能知道邮政编码或许能作为一个特征,告诉你步行化程度。比如这附近是不是高度步行化,你是否能步行去杂货店或者是学校,以及你是否需要驾驶汽车。有些人喜欢居住在以步行为主的区域,另外根据邮政编码还和富裕程度相关(在美国是这样的)。但在其它国家也可能体现出附近学校的水平有多好。<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy9kOTAwZDI2Yjg1MGQ1NWFiY2UzNmQ0YzhkYWY3MTMyNy5wbmc" alt="在这里插入图片描述"><br>在图上每一个画的小圆圈都可以是ReLU的一部分,也就是指修正线性单元,或者其它稍微非线性的函数。基于房屋面积和卧室数量,可以估算家庭人口,基于邮编,可以估测步行化程度或者学校的质量。最后你可能会这样想,这些决定人们乐意花费多少钱。</p><p>对于一个房子来说,这些都是与它息息相关的事情。在这个情景里,家庭人口、步行化程度以及学校的质量都能帮助你预测房屋的价格。以此为例, x是所有的这四个输入, y是你尝试预测的价格,把这些单个的神经元叠加在一起,我们就有了一个稍微大一点的神经网络。这显示了神经网络的神奇之处,虽然我已经描述了一个神经网络,它可以需要你得到房屋面积、步行化程度和学校的质量,或者其它影响价格的因素。<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy83YTBlMGQ0MGY0YmE4MGEwNDY2ZjBiZDdhYTlmODUzNy5wbmc" alt="在这里插入图片描述"><br>神经网络的一部分神奇之处在于,当你实现它之后,你要做的只是输入x就能得到输出y。因为它可以自己计算你训练集中样本的数目以及所有的中间过程。所以,你实际上要做的就是:这里有四个输入的神经网络,这输入的特征可能是房屋的大小、卧室的数量、邮政编码和区域的富裕程度。给出这些输入的特征之后,神经网络的工作就是预测对应的价格。同时也注意到这些被叫做隐藏单元圆圈,在一个神经网络中,它们每个都从输入的四个特征获得自身输入,比如说,第一个结点代表家庭人口,而家庭人口仅仅取决于x1和x2特征,换句话说,在神经网络中,你决定在这个结点中想要得到什么,然后用所有的四个输入来计算想要得到的。因此,我们说输入层和中间层被紧密的连接起来了。</p><p>值得注意的是神经网络给予了足够多的关于x和y的数据,给予了足够的训练样本有关x和y。神经网络非常擅长计算x从到y的精准映射函数。<br>这就是一个基础的神经网络。你可能发现你自己的神经网络在监督学习的环境下是如此的有效和强大,也就是说你只要尝试输入一个x即可把它映射成y,就好像我们在刚才房价预测的例子中看到的效果。</p><p>在下一个视频中,让我们复习一下更多监督学习的例子,有些例子会让你觉得你的网络会十分有用,并且你实际应用起来也是如此。</p><h2 id="1-3-神经网络的监督学习-Supervised-Learning-with-Neural-Networks"><a href="#1-3-神经网络的监督学习-Supervised-Learning-with-Neural-Networks" class="headerlink" title="1.3 神经网络的监督学习(Supervised Learning with Neural Networks)"></a>1.3 神经网络的监督学习(Supervised Learning with Neural Networks)</h2><p>关于神经网络也有很多的种类,考虑到它们的使用效果,有些使用起来恰到好处,但事实表明,到目前几乎所有由神经网络创造的经济价值,本质上都离不开一种叫做监督学习的机器学习类别,让我们举例看看。</p><p>在监督学习中你有一些输入x,你想学习到一个函数来映射到一些输出y如我们之前提到的房价预测的例子,你只要输入有关房屋的一些特征,试着去输出或者估计价格y,比如我们之前提到的房价预测的例子,你只要输入有关房屋的一些特征,试着去输出或者估计价格y。我们举一些其它的例子,来说明神经网络已经被高效应用到其它地方。<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy9lYzlmMTVkYTI1YzQwNzJlZWVkYzliYTdmYTM2M2Y4MC5wbmc" alt="在这里插入图片描述"><br>如今应用深度学习获利最多的一个领域,就是在线广告。这也许不是最鼓舞人心的,但真的很赚钱。具体就是通过在网站上输入一个广告的相关信息,因为也输入了用户的信息,于是网站就会考虑是否向你展示广告。</p><p>神经网络已经非常擅长预测你是否会点开这个广告,通过向用户展示最有可能点开的广告,这就是神经网络在很多家公司难以置信地提高获利的一种应用。因为有了这种向你展示你最有可能点击的广告的能力,而这一点击的行为的改变会直接影响到一些大型的在线广告公司的收入。</p><p>计算机视觉在过去的几年里也取得了长足的进步,这也多亏了深度学习。你可以输入一个图像,然后想输出一个索引,范围从1到1000来试着告诉你这张照片,它可能是,比方说,1000个不同的图像中的任何一个,所以你可能会选择用它来给照片打标签。</p><p>深度学习最近在语音识别方面的进步也是非常令人兴奋的,你现在可以将音频片段输入神经网络,然后让它输出文本记录。得益于深度学习,机器翻译也有很大的发展。你可以利用神经网络输入英语句子,接着输出一个中文句子。</p><p>在自动驾驶技术中,你可以输入一幅图像,就好像一个信息雷达展示汽车前方有什么,据此,你可以训练一个神经网络,来告诉汽车在马路上面具体的位置,这就是神经网络在自动驾驶系统中的一个关键成分。</p><p>那么深度学习系统已经可以创造如此多的价值,通过智能的选择,哪些作为x,哪些作为y,来针对于你当前的问题,然后拟合监督学习部分,往往是一个更大的系统,比如自动驾驶。这表明神经网络类型的轻微不同,也可以产生不同的应用,比如说,应用到我们在上一个视频提到的房地产领域,我们不就使用了一个普遍标准神经网络架构吗?</p><p>也许对于房地产和在线广告来说可能是相对的标准一些的神经网络,正如我们之前见到的。对于图像应用,我们经常在神经网络上使用卷积(Convolutional Neural Network),通常缩写为CNN。对于序列数据,例如音频,有一个时间组件,随着时间的推移,音频被播放出来,所以音频是最自然的表现。作为一维时间序列(两种英文说法one-dimensional time series / temporal sequence).对于序列数据,经常使用RNN,一种递归神经网络(Recurrent Neural Network),语言,英语和汉语字母表或单词都是逐个出现的,所以语言也是最自然的序列数据,因此更复杂的RNNs版本经常用于这些应用。</p><p>对于更复杂的应用比如自动驾驶,你有一张图片,可能会显示更多的CNN卷积神经网络结构,其中的雷达信息是完全不同的,你可能会有一个更定制的,或者一些更复杂的混合的神经网络结构。所以为了更具体地说明什么是标准的CNN和RNN结构,在文献中你可能见过这样的图片,这是一个标准的神经网络。<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy82Y2RhMzM2MWNlMTQyYjQzNDc1OTNkYjg0MmQ5NWVmMC5wbmc" alt="在这里插入图片描述"><br>你也可能见过这样的图片,这是一个卷积神经网络的例子。<img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy8xYmViZTBhYzQxNzE1ZWY4MTMyZjJkODAyOTY4NDk1Yy5wbmc" alt="在这里插入图片描述"><br>我们会在后面的课程了解这幅图的原理和实现,卷积网络(CNN)通常用于图像数据。</p><p>你可能也会看到这样的图片,而且你将在以后的课程中学习如何实现它。<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy80NjU2NjE3ZTMwZTdhZDQ0NDkwZmU2MDViMmU0OWU1Ni5wbmc" alt="在这里插入图片描述"><br>递归神经网络(RNN)非常适合这种一维序列,数据可能是一个时间组成部分。</p><p>你可能也听说过机器学习对于结构化数据和非结构化数据的应用,结构化数据意味着数据的基本数据库。例如在房价预测中,你可能有一个数据库,有专门的几列数据告诉你卧室的大小和数量,这就是结构化数据。或预测用户是否会点击广告,你可能会得到关于用户的信息,比如年龄以及关于广告的一些信息,然后对你的预测分类标注,这就是结构化数据,意思是每个特征,比如说房屋大小卧室数量,或者是一个用户的年龄,都有一个很好的定义。</p><p>相反非结构化数据是指比如音频,原始音频或者你想要识别的图像或文本中的内容。这里的特征可能是图像中的像素值或文本中的单个单词。<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy84NmEzOWQ0MGNiMTM4NDJjZDZjMDY0NjNjZDliNGE4My5wbmc" alt="在这里插入图片描述"></p><p>从历史经验上看,处理非结构化数据是很难的,与结构化数据比较,让计算机理解非结构化数据很难,而人类进化得非常善于理解音频信号和图像,文本是一个更近代的发明,但是人们真的很擅长解读非结构化数据。</p><p>神经网络的兴起就是这样最令人兴奋的事情之一,多亏了深度学习和神经网络,计算机现在能更好地解释非结构化数据,这是与几年前相比的结果,这为我们创造了机会。许多新的令人兴奋的应用被使用,语音识别、图像识别、自然语言文字处理,甚至可能比两三年前的还要多。因为人们天生就有本领去理解非结构化数据,你可能听说了神经网络更多在媒体非结构化数据的成功,当神经网络识别了一只猫时那真的很酷,我们都知道那意味着什么。</p><p>但结果也表明,神经网络在许多短期经济价值的创造,也是基于结构化数据的。比如更好的广告系统、更好的利润建议,还有更好的处理大数据的能力。许多公司不得不根据神经网络做出准确的预测。</p><p>因此在这门课中,我们将要讨论的许多技术都将适用,不论是对结构化数据还是非结构化数据。为了解释算法,我们将在使用非结构化数据的示例中多画一点图片,但正如你所想的,你自己团队里通过运用神经网络,我希望你能发现,神经网络算法对于结构化和非结构化数据都有用处。</p><p>神经网络已经改变了监督学习,正创造着巨大的经济价值,事实证明,基本的神经网络背后的技术理念大部分都离我们不遥远,有的是几十年,那么为什么他们现在才刚刚起步,效果那么好,下一集视频中我们将讨论为什么最近的神经网络已经成为你可以使用的强大工具。</p><h2 id="1-4-为什么深度学习会兴起?-Why-is-Deep-Learning-taking-off"><a href="#1-4-为什么深度学习会兴起?-Why-is-Deep-Learning-taking-off" class="headerlink" title="1.4 为什么深度学习会兴起?(Why is Deep Learning taking off?)"></a>1.4 为什么深度学习会兴起?(Why is Deep Learning taking off?)</h2><p>本节视频主要讲了推动深度学习变得如此热门的主要因素。包括数据规模、计算量及算法的创新。</p><p>深度学习和神经网络之前的基础技术理念已经存在大概几十年了,为什么它们现在才突然流行起来呢?本节课程主要讲述一些使得深度学习变得如此热门的主要驱动因素,这将会帮助你在你的组织机构内发现最好的时机来应用这些东西。</p><p>在过去的几年里,很多人都问我为什么深度学习能够如此有效。当我回答这个问题时,我通常给他们画个图,在水平轴上画一个形状,在此绘制出所有任务的数据量,而在垂直轴上,画出机器学习算法的性能。比如说准确率体现在垃圾邮件过滤或者广告点击预测,或者是神经网络在自动驾驶汽车时判断位置的准确性,根据图像可以发现,如果你把一个传统机器学习算法的性能画出来,作为数据量的一个函数,你可能得到一个弯曲的线,就像图中这样,它的性能一开始在增加更多数据时会上升,但是一段变化后它的性能就会像一个高原一样。假设你的水平轴拉的很长很长,它们不知道如何处理规模巨大的数据,而过去十年的社会里,我们遇到的很多问题只有相对较少的数据量。<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy83YTlhNjQyYzZjNzA5ZDE5ZWIwZGQyYjI0MWYxZWJhZi5wbmc" alt="在这里插入图片描述"><br>多亏数字化社会的来临,现在的数据量都非常巨大,我们花了很多时间活动在这些数字的领域,比如在电脑网站上、在手机软件上以及其它数字化的服务,它们都能创建数据,同时便宜的相机被配置到移动电话,还有加速仪及各类各样的传感器,同时在物联网领域我们也收集到了越来越多的数据。仅仅在过去的20年里对于很多应用,我们便收集到了大量的数据,远超过机器学习算法能够高效发挥它们优势的规模。</p><p>神经网络展现出的是,如果你训练一个小型的神经网络,那么这个性能可能会像下图黄色曲线表示那样;如果你训练一个稍微大一点的神经网络,比如说一个中等规模的神经网络(下图蓝色曲线),它在某些数据上面的性能也会更好一些;如果你训练一个非常大的神经网络,它就会变成下图绿色曲线那样,并且保持变得越来越好。因此可以注意到两点:如果你想要获得较高的性能体现,那么你有两个条件要完成,第一个是你需要训练一个规模足够大的神经网络,以发挥数据规模量巨大的优点,另外你需要能画到x轴的这个位置,所以你需要很多的数据。因此我们经常说规模一直在推动深度学习的进步,这里的规模指的也同时是神经网络的规模,我们需要一个带有许多隐藏单元的神经网络,也有许多的参数及关联性,就如同需要大规模的数据一样。事实上如今最可靠的方法来在神经网络上获得更好的性能,往往就是要么训练一个更大的神经网络,要么投入更多的数据,这只能在一定程度上起作用,因为最终你耗尽了数据,或者最终你的网络是如此大规模导致将要用太久的时间去训练,但是仅仅提升规模的的确确地让我们在深度学习的世界中摸索了很多时间。为了使这个图更加从技术上讲更精确一点,我在x<br>轴下面已经写明的数据量,这儿加上一个标签(label)量,通过添加这个标签量,也就是指在训练样本时,我们同时输入x和标签y接下来引入一点符号,使用小写的字母m表示训练集的规模,或者说训练样本的数量,这个小写字母m就横轴结合其他一些细节到这个图像中。</p><p><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy8yYjE0ZWRmY2IyMTIzNTExNWZjYTA1ODc5ZjhkOWRlMi5wbmc" alt="在这里插入图片描述"><br>在这个小的训练集中,各种算法的优先级事实上定义的也不是很明确,所以如果你没有大量的训练集,那效果会取决于你的特征工程能力,那将决定最终的性能。假设有些人训练出了一个SVM(支持向量机)表现的更接近正确特征,然而有些人训练的规模大一些,可能在这个小的训练集中SVM算法可以做的更好。因此你知道在这个图形区域的左边,各种算法之间的优先级并不是定义的很明确,最终的性能更多的是取决于你在用工程选择特征方面的能力以及算法处理方面的一些细节,只是在某些大数据规模非常庞大的训练集,也就是在右边这个m会非常的大时,我们能更加持续地看到更大的由神经网络控制的其它方法,因此如果你的任何某个朋友问你为什么神经网络这么流行,我会鼓励你也替他们画这样一个图形。</p><p> 所以可以这么说,在深度学习萌芽的初期,数据的规模以及计算量,局限在我们对于训练一个特别大的神经网络的能力,无论是在CPU还是GPU上面,那都使得我们取得了巨大的进步。但是渐渐地,尤其是在最近这几年,我们也见证了算法方面的极大创新。许多算法方面的创新,一直是在尝试着使得神经网络运行的更快。</p><p>作为一个具体的例子,神经网络方面的一个巨大突破是从sigmoid函数转换到一个ReLU函数,这个函数我们在之前的课程里提到过。<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy8xYTNkMjg4ZGMyNDNjYTljNWE3MGE2OTc5OTE4MGM0YS5wbmc" alt="在这里插入图片描述"><br>如果你无法理解刚才我说的某个细节,也不需要担心,可以知道的一个使用sigmoid函数和机器学习问题是,在这个区域,也就是这个sigmoid函数的梯度会接近零,所以学习的速度会变得非常缓慢,因为当你实现梯度下降以及梯度接近零的时候,参数会更新的很慢,所以学习的速率也会变的很慢,而通过改变这个被叫做激活函数的东西,神经网络换用这一个函数,叫做ReLU的函数(修正线性单元),ReLU它的梯度对于所有输入的负值都是零,因此梯度更加不会趋向逐渐减少到零。而这里的梯度,这条线的斜率在这左边是零,仅仅通过将Sigmod函数转换成ReLU函数,便能够使得一个叫做梯度下降(gradient descent)的算法运行的更快,这就是一个或许相对比较简单的算法创新的例子。但是根本上算法创新所带来的影响,实际上是对计算带来的优化,所以有很多像这样的例子,我们通过改变算法,使得代码运行的更快,这也使得我们能够训练规模更大的神经网络,或者是多端口的网络。即使我们从所有的数据中拥有了大规模的神经网络,快速计算显得更加重要的另一个原因是,训练你的神经网络的过程,很多时候是凭借直觉的,往往你对神经网络架构有了一个想法,于是你尝试写代码实现你的想法,然后让你运行一个试验环境来告诉你,你的神经网络效果有多好,通过参考这个结果再返回去修改你的神经网络里面的一些细节,然后你不断的重复上面的操作,当你的神经网络需要很长时间去训练,需要很长时间重复这一循环,在这里就有很大的区别,根据你的生产效率去构建更高效的神经网络。当你能够有一个想法,试一试,看效果如何。在10分钟内,或者也许要花上一整天,如果你训练你的神经网络用了一个月的时间,有时候发生这样的事情,也是值得的,因为你很快得到了一个结果。在10分钟内或者一天内,你应该尝试更多的想法,那极有可能使得你的神经网络在你的应用方面工作的更好、更快的计算,在提高速度方面真的有帮助,那样你就能更快地得到你的实验结果。这也同时帮助了神经网络的实验人员和有关项目的研究人员在深度学习的工作中迭代的更快,也能够更快的改进你的想法,所有这些都使得整个深度学习的研究社群变的如此繁荣,包括令人难以置信地发明新的算法和取得不间断的进步,这些都是开拓者在做的事情,这些力量使得深度学习不断壮大。<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy9lMjZkMThhODgyY2ZjNDg4MzcxMTg1NzJkY2E1MWM1Ni5wbmc" alt="在这里插入图片描述"></p><p>好消息是这些力量目前也正常不断的奏效,使得深度学习越来越好。研究表明我们的社会仍然正在抛出越来越多的数字化数据,或者用一些特殊的硬件来进行计算,比如说GPU,以及更快的网络连接各种硬件。我非常有信心,我们可以做一个超级大规模的神经网络,而计算的能力也会进一步的得到改善,还有算法相对的学习研究社区连续不断的在算法前沿产生非凡的创新。根据这些我们可以乐观地回答,同时对深度学习保持乐观态度,在接下来的这些年它都会变的越来越好。</p><h2 id="1-5-关于这门课-About-this-Course"><a href="#1-5-关于这门课-About-this-Course" class="headerlink" title="1.5 关于这门课(About this Course)"></a>1.5 关于这门课(About this Course)</h2><p>你的学习进度已经快接近这个专项课程的第一门课的第一周结尾了,首先,快速地介绍一下下周的学习内容:<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy80YmI0NDgzYzgwNzI3YWYxNTEwOGEyYzRhMjAwZjM3OC5wbmc" alt="在这里插入图片描述"><br>在第一个视频已经提到,这个专项有五门课程,目前正处于第一门课:神经网络与深度学习。在这门课中将教会你最重要的基础知识。当学习到第一门课末尾,你将学到如何建立一个深度神经网络并且使之奏效。</p><p>下面是关于第一门课的一些细节,这门课有四周的学习资料:</p><p>第一周:关于深度学习的介绍。在每一周的结尾也会有十个多选题用来检验自己对材料的理解;</p><p>第二周:关于神经网络的编程知识,了解神经网络的结构,逐步完善算法并思考如何使得神经网络高效地实现。从第二周开始做一些编程训练(付费项目),自己实现算法;</p><p>第三周:在学习了神经网络编程的框架之后,你将可以编写一个隐藏层神经网络,所以需要学习所有必须的关键概念来实现神经网络的工作;</p><p>第四周:建立一个深层的神经网络。</p><p>这段视频即将结束,希望在这段视频之后,你们可以看看课程网站的十道选择题来检查自己的理解,不必复习前面的知识,有的知识是你现在不知道的,可以不断尝试,直到全部做对以理解全部概念。</p><h2 id="1-6-课程资源-Course-Resources"><a href="#1-6-课程资源-Course-Resources" class="headerlink" title="1.6 课程资源(Course Resources)"></a>1.6 课程资源(Course Resources)</h2><p>我希望你们喜欢这门课程,为了帮助你们完成课程,本次课程将列举一些课程资源。</p><p>首先,如果你有任何疑问,或是想和其他同学讨论问题,或是想和包括我在内的教学人员讨论任何问题,或是想要归档一个错误,论坛是最好的去处,我和其他教学人员将定期关注论坛的内容。论坛也是一个你从同学那里得到问题答案的好地方,如果想要回答同学的问题,可以从课程首页来到论坛:<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL3d3dy5haS1zdGFydC5jb20vZGwyMDE3L2ltYWdlcy9lZjk4ZGNkNDY3YTYwM2NmOTA1MGI4ODdhOGRiZjkxYi5wbmc" alt="在这里插入图片描述"><br>点击论坛标签可以进入论坛</p><p>在论坛上是提问的最佳途径,但是出于一些原因,可能要直接联系我们,可以将邮件发送到这个地址,我们会尽力阅读每一份邮件并尝试解决普遍出现的问。由于邮件数量庞大,不一定能迅速回复每一封邮件。另外,一些公司会尝试给员工做深度学习培训,如果你们想对员工负责,聘请专家培训上百甚至更多员工深度学习,请用企业邮箱与我们联系。我们处在大学学术开发的初始阶段,如果你是大学领导或者是管理人员,并且希望在你们学校开设一门深度学习课程,请通过大学邮箱联系我们。邮箱地址如下,祝你们好运!</p><p>Contact us: <a href="mailto:[email protected]" target="_blank" rel="noopener">[email protected]</a></p><p>Companies: <a href="mailto:[email protected]" target="_blank" rel="noopener">[email protected]</a></p><p>Universities: <a href="mailto:[email protected]" target="_blank" rel="noopener">[email protected]</a></p><blockquote><p>转载自<深度学习笔记><a href="http://www.ai-start.com/dl2017/html/lesson1-week1.html" target="_blank" rel="noopener">http://www.ai-start.com/dl2017/html/lesson1-week1.html</a></p></blockquote>]]></content>
<tags>
<tag> 机器学习 </tag>
</tags>
</entry>
</search>