diff options
author | Hui Lan <lanhui@zjnu.edu.cn> | 2019-03-31 22:50:14 +0800 |
---|---|---|
committer | Hui Lan <lanhui@zjnu.edu.cn> | 2019-03-31 22:50:14 +0800 |
commit | 4ce937568716177d90ffbdde3c8ad1e5e935bc1d (patch) | |
tree | 0c253de2842c6004276b5843808dde42bfd0788b | |
parent | cc6155985ba7971224d0109d2c1d1501208b5ff9 (diff) |
updated lecture notes
-rw-r--r-- | LectureNotesOnPython.html | 398 | ||||
-rw-r--r-- | LectureNotesOnPython.rst | 391 |
2 files changed, 628 insertions, 161 deletions
diff --git a/LectureNotesOnPython.html b/LectureNotesOnPython.html index 966a1b5..34d3462 100644 --- a/LectureNotesOnPython.html +++ b/LectureNotesOnPython.html @@ -377,41 +377,48 @@ ul.auto-toc { <div class="contents topic" id="id1"> <p class="topic-title first">内容目录</p> <ul class="simple"> -<li><a class="reference internal" href="#python" id="id12">Python的发音纠正</a></li> -<li><a class="reference internal" href="#id2" id="id13">Python源流</a></li> -<li><a class="reference internal" href="#id3" id="id14">Python的关键词</a></li> -<li><a class="reference internal" href="#id4" id="id15">值的类型</a></li> -<li><a class="reference internal" href="#variable" id="id16">变量(Variable)</a></li> -<li><a class="reference internal" href="#mutable" id="id17">可变(mutable)类型与不可变类型</a></li> -<li><a class="reference internal" href="#id5" id="id18">数与格式化显示</a></li> -<li><a class="reference internal" href="#strings" id="id19">字符串(Strings)</a></li> -<li><a class="reference internal" href="#concatenation" id="id20">字符串相加(concatenation)</a></li> -<li><a class="reference internal" href="#slice" id="id21">子串(slice)</a></li> -<li><a class="reference internal" href="#id6" id="id22">搜索字符串</a></li> -<li><a class="reference internal" href="#string" id="id23">String类(对象)方法</a></li> -<li><a class="reference internal" href="#in" id="id24">in操作符</a></li> -<li><a class="reference internal" href="#id7" id="id25">字符串比较</a></li> -<li><a class="reference internal" href="#id8" id="id26">列表</a></li> -<li><a class="reference internal" href="#id9" id="id27">列表作为参数</a></li> -<li><a class="reference internal" href="#append" id="id28">注意区别 <tt class="docutils literal">append</tt> 与 <tt class="docutils literal">+</tt> 操作符</a><ul> -<li><a class="reference internal" href="#tdd-test-driven-development" id="id29">TDD - Test-driven Development</a></li> -<li><a class="reference internal" href="#id10" id="id30">计算复杂度</a></li> +<li><a class="reference internal" href="#python" id="id16">Python的发音纠正</a></li> +<li><a class="reference internal" href="#id2" id="id17">Python源流</a></li> +<li><a class="reference internal" href="#id3" id="id18">Python的关键词</a></li> +<li><a class="reference internal" href="#id4" id="id19">值的类型</a></li> +<li><a class="reference internal" href="#variable" id="id20">变量(Variable)</a></li> +<li><a class="reference internal" href="#mutable" id="id21">可变(mutable)类型与不可变类型</a></li> +<li><a class="reference internal" href="#id5" id="id22">数与格式化显示</a></li> +<li><a class="reference internal" href="#strings" id="id23">字符串(Strings)</a></li> +<li><a class="reference internal" href="#concatenation" id="id24">字符串相加(concatenation)</a></li> +<li><a class="reference internal" href="#slice" id="id25">子串(slice)</a></li> +<li><a class="reference internal" href="#id6" id="id26">搜索字符串</a></li> +<li><a class="reference internal" href="#string" id="id27">String类(对象)方法</a></li> +<li><a class="reference internal" href="#in" id="id28">in操作符</a></li> +<li><a class="reference internal" href="#id7" id="id29">字符串比较</a></li> +<li><a class="reference internal" href="#id8" id="id30">列表</a></li> +<li><a class="reference internal" href="#id9" id="id31">列表作为参数</a></li> +<li><a class="reference internal" href="#append" id="id32">注意区别 <tt class="docutils literal">append</tt> 与 <tt class="docutils literal">+</tt> 操作符</a><ul> +<li><a class="reference internal" href="#tdd-test-driven-development" id="id33">TDD - Test-driven Development</a></li> +<li><a class="reference internal" href="#id10" id="id34">计算复杂度</a></li> </ul> </li> -<li><a class="reference internal" href="#dictionary" id="id31">字典(Dictionary)</a><ul> -<li><a class="reference internal" href="#incremental-development" id="id32">递增开发(Incremental Development)</a></li> +<li><a class="reference internal" href="#dictionary" id="id35">字典(Dictionary)</a><ul> +<li><a class="reference internal" href="#incremental-development" id="id36">递增开发(Incremental Development)</a></li> +<li><a class="reference internal" href="#keyvalue" id="id37">key与value互换</a></li> </ul> </li> -<li><a class="reference internal" href="#id11" id="id33">参考</a></li> +<li><a class="reference internal" href="#id11" id="id38">函数</a><ul> +<li><a class="reference internal" href="#id12" id="id39">局部变量</a></li> +<li><a class="reference internal" href="#id13" id="id40">全局变量</a></li> +<li><a class="reference internal" href="#id14" id="id41">函数执行顺序</a></li> +</ul> +</li> +<li><a class="reference internal" href="#id15" id="id42">参考</a></li> </ul> </div> <div class="section" id="python"> -<h1><a class="toc-backref" href="#id12">Python的发音纠正</a></h1> +<h1><a class="toc-backref" href="#id16">Python的发音纠正</a></h1> <p>国人普遍把th发作s。 Not quite correct。</p> <p>ˈpī-ˌthän , -thən <a class="reference external" href="https://cn.bing.com/search?q=define%20python&tf=U2VydmljZT1EaWN0aW9uYXJ5QW5zd2VyVjIgU2NlbmFyaW89RGVmaW5pdGlvblNjZW5hcmlvIFBvc2l0aW9uPU5PUCBSYW5raW5nRGF0YT1UcnVlIEZvcmNlUGxhY2U9RmFsc2UgUGFpcnM9RGljdGlvbmFyeVdvcmQ6cHl0aG9uO3NjbjpEZWZpbml0aW9uU2NlbmFyaW87cDpRQVM7IHw%3d&hs=hyRBF0mYq9hrfQUq66DIZnFVta1ZGRfBiBks25oUguk%3d">pronounciation</a></p> </div> <div class="section" id="id2"> -<h1><a class="toc-backref" href="#id13">Python源流</a></h1> +<h1><a class="toc-backref" href="#id17">Python源流</a></h1> <p>Python之父Guido van Rossum,荷兰人,1956年生,1982年阿姆斯特丹大学获得 数学与计算机科学硕士学位。有过ABC语言的工作经验。1989年设计了Python语 言。</p> @@ -461,7 +468,7 @@ ul.auto-toc { </blockquote> </div> <div class="section" id="id3"> -<h1><a class="toc-backref" href="#id14">Python的关键词</a></h1> +<h1><a class="toc-backref" href="#id18">Python的关键词</a></h1> <div class="line-block"> <div class="line">def pass</div> <div class="line">from import</div> @@ -483,7 +490,7 @@ ul.auto-toc { <p>关键词被语言留用(reserved),无法作变量名。</p> </div> <div class="section" id="id4"> -<h1><a class="toc-backref" href="#id15">值的类型</a></h1> +<h1><a class="toc-backref" href="#id19">值的类型</a></h1> <p>所有的值都是对象。a = 5, help(a) a.bit_length()</p> <p>数字。1, 1.,1.1, .1, 1e1, 1e-1, 1E1, 1E-1</p> <dl class="docutils"> @@ -503,7 +510,7 @@ A list of objects</dd> <p>元组(tuple),字典(dict)。</p> </div> <div class="section" id="variable"> -<h1><a class="toc-backref" href="#id16">变量(Variable)</a></h1> +<h1><a class="toc-backref" href="#id20">变量(Variable)</a></h1> <p>是一个名字(name),是指向一个值(value)的名字。</p> <p>值存放在内存(memory)中的某个地址。</p> <p>尽量选有意义的简短的名字。比如,代表个数用n,代表索引用i,j,k。</p> @@ -592,7 +599,7 @@ A list of objects</dd> </ul> </div> <div class="section" id="mutable"> -<h1><a class="toc-backref" href="#id17">可变(mutable)类型与不可变类型</a></h1> +<h1><a class="toc-backref" href="#id21">可变(mutable)类型与不可变类型</a></h1> <p>字符串是不可变的(immutable)类型,不能在原内存地址改变。</p> <p>a = 'hello' 不可以原地修改a[0] = 'H'。需要修改a的值时,需要对a进行重新赋值a = 'Hello'。</p> <p>列表是可变(mutable)类型,能在原内存地址改变。</p> @@ -617,7 +624,8 @@ A list of objects</dd> </blockquote> </div> <div class="section" id="id5"> -<h1><a class="toc-backref" href="#id18">数与格式化显示</a></h1> +<h1><a class="toc-backref" href="#id22">数与格式化显示</a></h1> +<blockquote> <div class="line-block"> <div class="line">x = 3.1415926</div> </div> @@ -661,9 +669,10 @@ A list of objects</dd> <div class="line-block"> <div class="line">print('%f' % (x))</div> </div> +</blockquote> </div> <div class="section" id="strings"> -<h1><a class="toc-backref" href="#id19">字符串(Strings)</a></h1> +<h1><a class="toc-backref" href="#id23">字符串(Strings)</a></h1> <p>由字符组成。</p> <div class="line-block"> <div class="line">fruit = 'banana!'</div> @@ -719,7 +728,7 @@ A list of objects</dd> <p>以上 <tt class="docutils literal"># [start,stop,step]</tt> 代表注释(comment),注释以 <tt class="docutils literal">#</tt> 号开头。</p> </div> <div class="section" id="concatenation"> -<h1><a class="toc-backref" href="#id20">字符串相加(concatenation)</a></h1> +<h1><a class="toc-backref" href="#id24">字符串相加(concatenation)</a></h1> <p>输出Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack</p> <div class="line-block"> <div class="line">prefixes = 'JKLMNOPQ'</div> @@ -738,7 +747,7 @@ A list of objects</dd> </div> </div> <div class="section" id="slice"> -<h1><a class="toc-backref" href="#id21">子串(slice)</a></h1> +<h1><a class="toc-backref" href="#id25">子串(slice)</a></h1> <p>s[n:m],其中n或m可省略。 包括第n个字符,不包括第m个字符。(索引自0开始)</p> <div class="line-block"> @@ -762,7 +771,7 @@ A list of objects</dd> </div> </div> <div class="section" id="id6"> -<h1><a class="toc-backref" href="#id22">搜索字符串</a></h1> +<h1><a class="toc-backref" href="#id26">搜索字符串</a></h1> <div class="line-block"> <div class="line">def find(word, c):</div> <div class="line-block"> @@ -788,7 +797,7 @@ A list of objects</dd> <p>练习:用上面三参数的find来做。</p> </div> <div class="section" id="string"> -<h1><a class="toc-backref" href="#id23">String类(对象)方法</a></h1> +<h1><a class="toc-backref" href="#id27">String类(对象)方法</a></h1> <div class="line-block"> <div class="line">upper()</div> <div class="line">lower()</div> @@ -801,14 +810,14 @@ A list of objects</dd> </div> </div> <div class="section" id="in"> -<h1><a class="toc-backref" href="#id24">in操作符</a></h1> +<h1><a class="toc-backref" href="#id28">in操作符</a></h1> <p>'a' in 'banana' 'seed' in 'banana'</p> <p>练习:写出下面的函数,使得 in_both('apples', 'oranges')返回'aes'</p> </div> <div class="section" id="id7"> -<h1><a class="toc-backref" href="#id25">字符串比较</a></h1> +<h1><a class="toc-backref" href="#id29">字符串比较</a></h1> <p>字典序(alphabetical order)。大写字母排在小写字母前。</p> <p>字符串之间可以有以下对比操作:</p> <div class="line-block"> @@ -824,7 +833,7 @@ in_both('apples', 'oranges')返回'aes'</p> <p>即兴定义函数,制造一个长度不小于4的密码。</p> </div> <div class="section" id="id8"> -<h1><a class="toc-backref" href="#id26">列表</a></h1> +<h1><a class="toc-backref" href="#id30">列表</a></h1> <p>语言的内置(built-in)类型。注意与String类比,index也是从0开始, in操作符, 求长度,获得字串,遍历操作类似。</p> <blockquote> <div class="line-block"> @@ -865,7 +874,7 @@ in_both('apples', 'oranges')返回'aes'</p> <dd>return 2*x</dd> </dl> <p>list(map(f, [1,2]]))</p> -<p>filter方法,从几个值中选择符合条件的几个值</p> +<p>filter方法,从几个值中选择符合条件的几个值。</p> <blockquote> <div class="line-block"> <div class="line">def f(x):</div> @@ -942,7 +951,7 @@ a与b是指向[1,2,3]的两个references。 error-prone(易错)</p> </div> <div class="section" id="id9"> -<h1><a class="toc-backref" href="#id27">列表作为参数</a></h1> +<h1><a class="toc-backref" href="#id31">列表作为参数</a></h1> <blockquote> <div class="line-block"> <div class="line">def delete_head(t):</div> @@ -958,7 +967,7 @@ error-prone(易错)</p> </blockquote> </div> <div class="section" id="append"> -<h1><a class="toc-backref" href="#id28">注意区别 <tt class="docutils literal">append</tt> 与 <tt class="docutils literal">+</tt> 操作符</a></h1> +<h1><a class="toc-backref" href="#id32">注意区别 <tt class="docutils literal">append</tt> 与 <tt class="docutils literal">+</tt> 操作符</a></h1> <blockquote> <div class="line-block"> <div class="line">t1 = [1, 2]</div> @@ -985,18 +994,53 @@ error-prone(易错)</p> </dl> </blockquote> <div class="section" id="tdd-test-driven-development"> -<h2><a class="toc-backref" href="#id29">TDD - Test-driven Development</a></h2> +<h2><a class="toc-backref" href="#id33">TDD - Test-driven Development</a></h2> <p>测试驱动开发。 My favourite。 刺激有挑战性。 帮助厘清需求。 帮助编写代码。</p> -<p>推荐使用pytest。如何安装? 使用命令 <tt class="docutils literal">pip install pytest</tt></p> +<p>推荐使用pytest。如何安装? 使用命令 <tt class="docutils literal">pip install pytest</tt>。</p> +<p>在 <tt class="docutils literal">test_cases.py</tt> 写如下测试用例。然后在命令行运行: <tt class="docutils literal">python <span class="pre">-m</span> pytest test_cases.py</tt> 。</p> +<pre class="code python literal-block"> +<span class="comment single"># Copyright (c) Hui Lan 2019</span> + +<span class="keyword namespace">import</span> <span class="name namespace">random</span> +<span class="keyword namespace">import</span> <span class="name namespace">string</span> + +<span class="keyword">def</span> <span class="name function">make_password</span><span class="punctuation">(</span><span class="name">n</span><span class="punctuation">):</span> + <span class="literal string doc">''' + Return a string of length n consisting of a combination of + letters, digits and special characters. Note that each password + must have at least one lower case letter, one upper case letter, + one digit and one special charater. Return an empty string if n + is less than 4. + '''</span> + + <span class="keyword">if</span> <span class="name">n</span> <span class="operator"><</span> <span class="literal number integer">4</span><span class="punctuation">:</span> + <span class="keyword">return</span> <span class="literal string single">''</span> + + <span class="name">password</span> <span class="operator">=</span> <span class="name">random</span><span class="operator">.</span><span class="name">choice</span><span class="punctuation">(</span><span class="name">string</span><span class="operator">.</span><span class="name">ascii_lowercase</span><span class="punctuation">)</span> <span class="operator">+</span> \ + <span class="name">random</span><span class="operator">.</span><span class="name">choice</span><span class="punctuation">(</span><span class="name">string</span><span class="operator">.</span><span class="name">ascii_uppercase</span><span class="punctuation">)</span> <span class="operator">+</span> \ + <span class="name">random</span><span class="operator">.</span><span class="name">choice</span><span class="punctuation">(</span><span class="name">string</span><span class="operator">.</span><span class="name">digits</span><span class="punctuation">)</span> <span class="operator">+</span> \ + <span class="name">random</span><span class="operator">.</span><span class="name">choice</span><span class="punctuation">(</span><span class="name">string</span><span class="operator">.</span><span class="name">punctuation</span><span class="punctuation">)</span> <span class="operator">+</span> \ + <span class="literal string single">''</span><span class="operator">.</span><span class="name">join</span><span class="punctuation">([</span><span class="name">random</span><span class="operator">.</span><span class="name">choice</span><span class="punctuation">(</span><span class="name">string</span><span class="operator">.</span><span class="name">ascii_letters</span> <span class="operator">+</span> <span class="name">string</span><span class="operator">.</span><span class="name">digits</span> <span class="operator">+</span> <span class="name">string</span><span class="operator">.</span><span class="name">punctuation</span><span class="punctuation">)</span> <span class="keyword">for</span> <span class="name">i</span> <span class="operator word">in</span> <span class="name builtin">range</span><span class="punctuation">(</span><span class="name">n</span><span class="operator">-</span><span class="literal number integer">4</span><span class="punctuation">)])</span> + + <span class="keyword">return</span> <span class="literal string single">''</span><span class="operator">.</span><span class="name">join</span><span class="punctuation">(</span><span class="name">random</span><span class="operator">.</span><span class="name">sample</span><span class="punctuation">(</span><span class="name">password</span><span class="punctuation">,</span> <span class="name">n</span><span class="punctuation">))</span> <span class="comment single"># shuffle password then return</span> + + + + +<span class="keyword">if</span> <span class="name variable magic">__name__</span> <span class="operator">==</span> <span class="literal string single">'__main__'</span><span class="punctuation">:</span> + <span class="keyword">for</span> <span class="name">n</span> <span class="operator word">in</span> <span class="name builtin">range</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">20</span><span class="punctuation">):</span> + <span class="name">pwd</span> <span class="operator">=</span> <span class="name">make_password</span><span class="punctuation">(</span><span class="name">n</span><span class="punctuation">)</span> + <span class="keyword">print</span><span class="punctuation">(</span><span class="name">pwd</span><span class="punctuation">)</span> +</pre> </div> <div class="section" id="id10"> -<h2><a class="toc-backref" href="#id30">计算复杂度</a></h2> +<h2><a class="toc-backref" href="#id34">计算复杂度</a></h2> <p>用Big O表述复杂度。O(n), O(n^2), O(n^3)。</p> <p>密码实验回顾。</p> </div> </div> <div class="section" id="dictionary"> -<h1><a class="toc-backref" href="#id31">字典(Dictionary)</a></h1> +<h1><a class="toc-backref" href="#id35">字典(Dictionary)</a></h1> <p>Mutable数据类型。</p> <p>实际开发中超级有用。</p> <blockquote> @@ -1020,66 +1064,228 @@ error-prone(易错)</p> <p>key-value pair (item)</p> <p>item的顺序不可预测,不是按照创建时的顺序。</p> <div class="section" id="incremental-development"> -<h2><a class="toc-backref" href="#id32">递增开发(Incremental Development)</a></h2> +<h2><a class="toc-backref" href="#id36">递增开发(Incremental Development)</a></h2> <p>每次完成一小点。从易到难。</p> <p>练习:给定一个字符串,数出每个字母出现的频率。</p> -<blockquote> -<div class="line-block"> -<div class="line">def histogram(s):</div> -<div class="line-block"> -<div class="line">''' Cannot pass any test cases. '''</div> -<div class="line">pass</div> -</div> -</div> -<div class="line-block"> -<div class="line">def histogram(s):</div> -<div class="line-block"> -<div class="line">''' Can pass the test case in which s is an empty string. '''</div> -<div class="line">d = {}</div> -<div class="line">return d</div> -</div> -</div> -<div class="line-block"> -<div class="line">def histogram(s):</div> -<div class="line-block"> -<div class="line">''' Can pass the test cases in which all characters in s are unique. '''</div> -<div class="line">d = {}</div> -<div class="line">for c in s:</div> -<div class="line-block"> -<div class="line">d[c] = 1</div> -</div> -<div class="line">return d</div> -</div> -</div> -<div class="line-block"> -<div class="line">def histogram(s):</div> -<div class="line-block"> -<div class="line">''' Can pass all test cases. '''</div> -<div class="line">d = {}</div> -<div class="line">for c in s:</div> -<div class="line-block"> -<div class="line">if c not in d:</div> -<div class="line-block"> -<div class="line">d[c] = 1</div> -</div> -<div class="line">else:</div> -<div class="line-block"> -<div class="line">d[c] += 1</div> -</div> +<pre class="code python literal-block"> +<span class="keyword">def</span> <span class="name function">histogram</span><span class="punctuation">(</span><span class="name">s</span><span class="punctuation">):</span> + <span class="literal string doc">''' Cannot pass any test cases. '''</span> + <span class="keyword">pass</span> + +<span class="keyword">def</span> <span class="name function">histogram</span><span class="punctuation">(</span><span class="name">s</span><span class="punctuation">):</span> + <span class="literal string doc">''' Can pass the test case in which s is an empty string. '''</span> + <span class="name">d</span> <span class="operator">=</span> <span class="punctuation">{}</span> + <span class="keyword">return</span> <span class="name">d</span> + +<span class="keyword">def</span> <span class="name function">histogram</span><span class="punctuation">(</span><span class="name">s</span><span class="punctuation">):</span> + <span class="literal string doc">''' Can pass the test cases in which all characters in s are unique. '''</span> + <span class="name">d</span> <span class="operator">=</span> <span class="punctuation">{}</span> + <span class="keyword">for</span> <span class="name">c</span> <span class="operator word">in</span> <span class="name">s</span><span class="punctuation">:</span> + <span class="name">d</span><span class="punctuation">[</span><span class="name">c</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">1</span> + <span class="keyword">return</span> <span class="name">d</span> + +<span class="keyword">def</span> <span class="name function">histogram</span><span class="punctuation">(</span><span class="name">s</span><span class="punctuation">):</span> + <span class="literal string doc">''' Can pass all test cases. '''</span> + <span class="name">d</span> <span class="operator">=</span> <span class="punctuation">{}</span> + <span class="keyword">for</span> <span class="name">c</span> <span class="operator word">in</span> <span class="name">s</span><span class="punctuation">:</span> + <span class="keyword">if</span> <span class="name">c</span> <span class="operator word">not</span> <span class="operator word">in</span> <span class="name">d</span><span class="punctuation">:</span> + <span class="name">d</span><span class="punctuation">[</span><span class="name">c</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">1</span> + <span class="keyword">else</span><span class="punctuation">:</span> + <span class="name">d</span><span class="punctuation">[</span><span class="name">c</span><span class="punctuation">]</span> <span class="operator">+=</span> <span class="literal number integer">1</span> + <span class="keyword">return</span> <span class="name">d</span> + + +<span class="name">h</span> <span class="operator">=</span> <span class="name">histogram</span><span class="punctuation">(</span><span class="literal string single">'good'</span><span class="punctuation">)</span> +<span class="keyword">print</span><span class="punctuation">(</span><span class="name">h</span><span class="punctuation">)</span> +</pre> +<p>练习:给定一个字符串,数出每个单词出现的频率。</p> +<p>练习:给定一个新闻文本,数出每个单词出现的频率。考虑以下方面,(1)只考虑字典里有的单词。(2)单词周围如有标点符号,要先移除。</p> +<pre class="code python literal-block"> +<span class="comment single"># Copyright (C) 2019 Hui Lan</span> +<span class="comment single"># The following line fixes SyntaxError: Non-UTF-8 code starting with ...</span> +<span class="comment single"># coding=utf8</span> + +<span class="keyword">def</span> <span class="name function">file2lst</span><span class="punctuation">(</span><span class="name">fname</span><span class="punctuation">):</span> + <span class="literal string doc">''' Return a list where each element is a word from fname. '''</span> + <span class="name">L</span> <span class="operator">=</span> <span class="punctuation">[]</span> + <span class="name">f</span> <span class="operator">=</span> <span class="name builtin">open</span><span class="punctuation">(</span><span class="name">fname</span><span class="punctuation">)</span> + <span class="keyword">for</span> <span class="name">line</span> <span class="operator word">in</span> <span class="name">f</span><span class="punctuation">:</span> + <span class="name">line</span> <span class="operator">=</span> <span class="name">line</span><span class="operator">.</span><span class="name">strip</span><span class="punctuation">()</span> + <span class="name">lst</span> <span class="operator">=</span> <span class="name">line</span><span class="operator">.</span><span class="name">split</span><span class="punctuation">()</span> + <span class="keyword">for</span> <span class="name">x</span> <span class="operator word">in</span> <span class="name">lst</span><span class="punctuation">:</span> + <span class="name">L</span><span class="operator">.</span><span class="name">append</span><span class="punctuation">(</span><span class="name">x</span><span class="punctuation">)</span> + <span class="name">f</span><span class="operator">.</span><span class="name">close</span><span class="punctuation">()</span> + <span class="keyword">return</span> <span class="name">L</span> + + +<span class="keyword">def</span> <span class="name function">lst2dict</span><span class="punctuation">(</span><span class="name">lst</span><span class="punctuation">):</span> + <span class="literal string doc">''' Return a dictionary given list lst. Each key is an element in the lst. + The value is always 1.'''</span> + <span class="name">d</span> <span class="operator">=</span> <span class="punctuation">{}</span> + <span class="keyword">for</span> <span class="name">w</span> <span class="operator word">in</span> <span class="name">lst</span><span class="punctuation">:</span> + <span class="name">d</span><span class="punctuation">[</span><span class="name">w</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">1</span> + <span class="keyword">return</span> <span class="name">d</span> + + +<span class="keyword namespace">import</span> <span class="name namespace">string</span> +<span class="keyword">def</span> <span class="name function">remove_punctuation</span><span class="punctuation">(</span><span class="name">s</span><span class="punctuation">):</span> + <span class="name">p</span> <span class="operator">=</span> <span class="literal string single">',.:’“”'</span> <span class="operator">+</span> <span class="name">string</span><span class="operator">.</span><span class="name">punctuation</span> + <span class="name">t</span> <span class="operator">=</span> <span class="literal string single">''</span> + <span class="keyword">for</span> <span class="name">c</span> <span class="operator word">in</span> <span class="name">s</span><span class="punctuation">:</span> + <span class="keyword">if</span> <span class="operator word">not</span> <span class="name">c</span> <span class="operator word">in</span> <span class="name">p</span><span class="punctuation">:</span> + <span class="name">t</span> <span class="operator">+=</span> <span class="name">c</span> + <span class="keyword">elif</span> <span class="name">c</span> <span class="operator">==</span> <span class="literal string single">'’'</span><span class="punctuation">:</span> <span class="comment single"># handle the case such as May’s</span> + <span class="keyword">return</span> <span class="name">t</span> + <span class="keyword">return</span> <span class="name">t</span> + +<span class="keyword">def</span> <span class="name function">word_frequency</span><span class="punctuation">(</span><span class="name">fname</span><span class="punctuation">,</span> <span class="name">english_dictionary</span><span class="punctuation">):</span> + <span class="literal string doc">''' Return a dictionary where each key is a word both in the file fname and in + the dictionary english_dictionary, and the corresponding value is the frequency + of that word.'''</span> + <span class="name">d</span> <span class="operator">=</span> <span class="punctuation">{}</span> + <span class="name">L</span> <span class="operator">=</span> <span class="name">file2lst</span><span class="punctuation">(</span><span class="name">fname</span><span class="punctuation">)</span> + <span class="keyword">for</span> <span class="name">x</span> <span class="operator word">in</span> <span class="name">L</span><span class="punctuation">:</span> + <span class="name">x</span> <span class="operator">=</span> <span class="name">remove_punctuation</span><span class="punctuation">(</span><span class="name">x</span><span class="operator">.</span><span class="name">lower</span><span class="punctuation">())</span> + <span class="keyword">if</span> <span class="name">x</span> <span class="operator word">in</span> <span class="name">english_dictionary</span><span class="punctuation">:</span> + <span class="keyword">if</span> <span class="operator word">not</span> <span class="name">x</span> <span class="operator word">in</span> <span class="name">d</span><span class="punctuation">:</span> + <span class="name">d</span><span class="punctuation">[</span><span class="name">x</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">1</span> + <span class="keyword">else</span><span class="punctuation">:</span> + <span class="name">d</span><span class="punctuation">[</span><span class="name">x</span><span class="punctuation">]</span> <span class="operator">+=</span> <span class="literal number integer">1</span> + <span class="keyword">return</span> <span class="name">d</span> + + +<span class="keyword">def</span> <span class="name function">sort_by_value</span><span class="punctuation">(</span><span class="name">d</span><span class="punctuation">):</span> + <span class="literal string doc">''' Return a sorted list of tuples, each tuple containing a key and a value. + Note that the tuples are order in descending order of the value.'''</span> + <span class="keyword namespace">import</span> <span class="name namespace">operator</span> + <span class="name">lst</span> <span class="operator">=</span> <span class="name builtin">sorted</span><span class="punctuation">(</span><span class="name">d</span><span class="operator">.</span><span class="name">items</span><span class="punctuation">(),</span> <span class="name">key</span><span class="operator">=</span><span class="name">operator</span><span class="operator">.</span><span class="name">itemgetter</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">),</span> <span class="name">reverse</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">)</span> + <span class="keyword">return</span> <span class="name">lst</span> + + +<span class="keyword">if</span> <span class="name variable magic">__name__</span> <span class="operator">==</span> <span class="literal string single">'__main__'</span><span class="punctuation">:</span> + <span class="name">ed</span> <span class="operator">=</span> <span class="name">lst2dict</span><span class="punctuation">(</span><span class="name">file2lst</span><span class="punctuation">(</span><span class="literal string single">'words.txt'</span><span class="punctuation">))</span> <span class="comment single"># from http://greenteapress.com/thinkpython2/code/words.txt</span> + <span class="name">d</span> <span class="operator">=</span> <span class="name">word_frequency</span><span class="punctuation">(</span><span class="literal string single">'brexit-news.txt'</span><span class="punctuation">,</span> <span class="name">ed</span><span class="punctuation">)</span> + <span class="name">lst</span> <span class="operator">=</span> <span class="name">sort_by_value</span><span class="punctuation">(</span><span class="name">d</span><span class="punctuation">)</span> + <span class="keyword">for</span> <span class="name">x</span> <span class="operator word">in</span> <span class="name">lst</span><span class="punctuation">:</span> + <span class="keyword">print</span><span class="punctuation">(</span><span class="literal string single">'</span><span class="literal string interpol">%s</span><span class="literal string single"> (</span><span class="literal string interpol">%d</span><span class="literal string single">)'</span> <span class="operator">%</span> <span class="punctuation">(</span><span class="name">x</span><span class="punctuation">[</span><span class="literal number integer">0</span><span class="punctuation">],</span> <span class="name">x</span><span class="punctuation">[</span><span class="literal number integer">1</span><span class="punctuation">]))</span> +</pre> +</div> +<div class="section" id="keyvalue"> +<h2><a class="toc-backref" href="#id37">key与value互换</a></h2> +<p>注意到在原来的字典中一个value可能对应多个key的值。比如 <tt class="docutils literal">d = <span class="pre">{'a':1,</span> <span class="pre">'b':2,</span> <span class="pre">'c':2}</span></tt> 中,2就对应两个key,'b'与'c'。</p> +<pre class="code python literal-block"> +<span class="keyword">def</span> <span class="name function">inverse_dictionary</span><span class="punctuation">(</span><span class="name">d</span><span class="punctuation">):</span> + <span class="name">d2</span> <span class="operator">=</span> <span class="punctuation">{}</span> + <span class="keyword">for</span> <span class="name">k</span> <span class="operator word">in</span> <span class="name">d</span><span class="punctuation">:</span> + <span class="name">v</span> <span class="operator">=</span> <span class="name">d</span><span class="punctuation">[</span><span class="name">k</span><span class="punctuation">]</span> + <span class="keyword">if</span> <span class="operator word">not</span> <span class="name">v</span> <span class="operator word">in</span> <span class="name">d2</span><span class="punctuation">:</span> + <span class="name">d2</span><span class="punctuation">[</span><span class="name">v</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="punctuation">[</span><span class="name">k</span><span class="punctuation">]</span> + <span class="keyword">else</span><span class="punctuation">:</span> + <span class="name">d2</span><span class="punctuation">[</span><span class="name">v</span><span class="punctuation">]</span><span class="operator">.</span><span class="name">append</span><span class="punctuation">(</span><span class="name">k</span><span class="punctuation">)</span> + <span class="keyword">return</span> <span class="name">d2</span> + + + +<span class="name">d</span> <span class="operator">=</span> <span class="punctuation">{</span><span class="literal string single">'a'</span><span class="punctuation">:</span><span class="literal number integer">1</span><span class="punctuation">,</span> <span class="literal string single">'b'</span><span class="punctuation">:</span><span class="literal number integer">2</span><span class="punctuation">,</span> <span class="literal string single">'c'</span><span class="punctuation">:</span><span class="literal number integer">2</span><span class="punctuation">}</span> +<span class="name">d2</span> <span class="operator">=</span> <span class="name">inverse_dictionary</span><span class="punctuation">(</span><span class="name">d</span><span class="punctuation">)</span> +<span class="keyword">print</span><span class="punctuation">(</span><span class="name">d2</span><span class="punctuation">)</span> +</pre> +<p>练习: 用 <tt class="docutils literal">inverse_dictionary</tt> 对上面 <tt class="docutils literal">d = <span class="pre">word_frequency('brexit-news.txt',</span> ed)</tt> 产生的 <tt class="docutils literal">d</tt> 进行转化。然后按照单词出现频率从高到低把所有单词都显示出来。每行显示一个频率内的所有单词。</p> +<pre class="code python literal-block"> +<span class="name">d2</span> <span class="operator">=</span> <span class="name">inverse_dictionary</span><span class="punctuation">(</span><span class="name">d</span><span class="punctuation">)</span> +<span class="keyword">for</span> <span class="name">k</span> <span class="operator word">in</span> <span class="name builtin">sorted</span><span class="punctuation">(</span><span class="name">d2</span><span class="operator">.</span><span class="name">keys</span><span class="punctuation">(),</span> <span class="name">reverse</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">):</span> + <span class="keyword">print</span><span class="punctuation">(</span><span class="literal string single">'</span><span class="literal string interpol">%d</span><span class="literal string single"> </span><span class="literal string interpol">%s</span><span class="literal string single">'</span> <span class="operator">%</span> <span class="punctuation">(</span><span class="name">k</span><span class="punctuation">,</span> <span class="literal string single">' '</span><span class="operator">.</span><span class="name">join</span><span class="punctuation">(</span><span class="name">d2</span><span class="punctuation">[</span><span class="name">k</span><span class="punctuation">])))</span> +</pre> +<p>练习: 使用 <tt class="docutils literal">setdefault</tt> 方法对上面的 <tt class="docutils literal">inverse_dictionary</tt> 进行简化 (减少行数)。</p> +<pre class="code python literal-block"> +<span class="keyword">def</span> <span class="name function">inverse_dictionary</span><span class="punctuation">(</span><span class="name">d</span><span class="punctuation">):</span> + <span class="name">d2</span> <span class="operator">=</span> <span class="punctuation">{}</span> + <span class="keyword">for</span> <span class="name">k</span> <span class="operator word">in</span> <span class="name">d</span><span class="punctuation">:</span> + <span class="name">v</span> <span class="operator">=</span> <span class="name">d</span><span class="punctuation">[</span><span class="name">k</span><span class="punctuation">]</span> + <span class="name">d2</span><span class="operator">.</span><span class="name">setdefault</span><span class="punctuation">(</span><span class="name">v</span><span class="punctuation">,</span> <span class="punctuation">[])</span><span class="operator">.</span><span class="name">append</span><span class="punctuation">(</span><span class="name">k</span><span class="punctuation">)</span> + + <span class="keyword">return</span> <span class="name">d2</span> +</pre> </div> -<div class="line">return d</div> </div> +<div class="section" id="id11"> +<h1><a class="toc-backref" href="#id38">函数</a></h1> +<p>函数 <tt class="docutils literal">unique_words</tt> 与 <tt class="docutils literal">unique_words2</tt> 哪个运行速度快?</p> +<pre class="code python literal-block"> +<span class="keyword">def</span> <span class="name function">unique_words</span><span class="punctuation">(</span><span class="name">lst</span><span class="punctuation">):</span> + <span class="name">d</span> <span class="operator">=</span> <span class="punctuation">{}</span> + <span class="keyword">for</span> <span class="name">x</span> <span class="operator word">in</span> <span class="name">lst</span><span class="punctuation">:</span> + <span class="name">d</span><span class="punctuation">[</span><span class="name">x</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">1</span> + <span class="keyword">return</span> <span class="name builtin">sorted</span><span class="punctuation">(</span><span class="name">d</span><span class="operator">.</span><span class="name">keys</span><span class="punctuation">())</span> + +<span class="keyword">def</span> <span class="name function">unique_words2</span><span class="punctuation">(</span><span class="name">lst</span><span class="punctuation">):</span> + <span class="keyword">return</span> <span class="name builtin">sorted</span><span class="punctuation">(</span><span class="name builtin">list</span><span class="punctuation">(</span><span class="name builtin">set</span><span class="punctuation">(</span><span class="name">lst</span><span class="punctuation">)))</span> + + +<span class="name">N</span> <span class="operator">=</span> <span class="literal number integer">10000000</span> +<span class="keyword">print</span><span class="punctuation">(</span><span class="name">unique_words</span><span class="punctuation">([</span><span class="literal string single">'hello'</span><span class="punctuation">,</span> <span class="literal string single">'world'</span><span class="punctuation">,</span> <span class="literal string single">'am'</span><span class="punctuation">,</span> <span class="literal string single">'he'</span><span class="punctuation">]</span> <span class="operator">*</span> <span class="name">N</span><span class="punctuation">))</span> +<span class="keyword">print</span><span class="punctuation">(</span><span class="name">unique_words2</span><span class="punctuation">([</span><span class="literal string single">'hello'</span><span class="punctuation">,</span> <span class="literal string single">'world'</span><span class="punctuation">,</span> <span class="literal string single">'am'</span><span class="punctuation">,</span> <span class="literal string single">'he'</span><span class="punctuation">]</span> <span class="operator">*</span> <span class="name">N</span><span class="punctuation">))</span> +</pre> +<div class="section" id="id12"> +<h2><a class="toc-backref" href="#id39">局部变量</a></h2> +<p>在函数之内。函数执行结束,局部变量消失。</p> </div> -<div class="line-block"> -<div class="line">h = histogram('good')</div> -<div class="line">print(h)</div> +<div class="section" id="id13"> +<h2><a class="toc-backref" href="#id40">全局变量</a></h2> +<p>全局变量位于函数之外,模块之内。全局变量对所有模块内的函数可见(可读)。如果在函数内要对全局变量重新赋值,那么要先用 <tt class="docutils literal">global</tt> 声明之 (declare)。</p> +<pre class="code python literal-block"> +<span class="name">verbose</span> <span class="operator">=</span> <span class="name builtin pseudo">True</span> + +<span class="keyword">def</span> <span class="name function">example1</span><span class="punctuation">():</span> + <span class="keyword">if</span> <span class="name">verbose</span><span class="punctuation">:</span> + <span class="keyword">print</span><span class="punctuation">(</span><span class="literal string single">'Running example1'</span><span class="punctuation">)</span> + +<span class="keyword">def</span> <span class="name function">example2</span><span class="punctuation">():</span> + <span class="name">verbose</span> <span class="operator">=</span> <span class="name builtin pseudo">False</span> <span class="comment single"># a NEW local variable verbose</span> + <span class="keyword">if</span> <span class="name">verbose</span><span class="punctuation">:</span> + <span class="keyword">print</span><span class="punctuation">(</span><span class="literal string single">'Running example2'</span><span class="punctuation">)</span> + +<span class="keyword">def</span> <span class="name function">example3</span><span class="punctuation">():</span> + <span class="keyword">global</span> <span class="name">verbose</span> <span class="comment single"># I am actually going to use the global variable verbose; don't create a local one.</span> + <span class="name">verbose</span> <span class="operator">=</span> <span class="name builtin pseudo">False</span> + <span class="keyword">if</span> <span class="name">verbose</span><span class="punctuation">:</span> + <span class="keyword">print</span><span class="punctuation">(</span><span class="literal string single">'Running example3'</span><span class="punctuation">)</span> + + + +<span class="keyword">print</span><span class="punctuation">(</span><span class="name">verbose</span><span class="punctuation">)</span> +<span class="name">example1</span><span class="punctuation">()</span> + +<span class="keyword">print</span><span class="punctuation">(</span><span class="name">verbose</span><span class="punctuation">)</span> +<span class="name">example2</span><span class="punctuation">()</span> +<span class="name">example1</span><span class="punctuation">()</span> + +<span class="keyword">print</span><span class="punctuation">(</span><span class="name">verbose</span><span class="punctuation">)</span> +<span class="name">example3</span><span class="punctuation">()</span> +<span class="name">example1</span><span class="punctuation">()</span> + +<span class="keyword">print</span><span class="punctuation">(</span><span class="name">verbose</span><span class="punctuation">)</span> +</pre> +<p>全局的列表与字典,如果只需改变其内容,而不是重新赋值,则不需要用 <tt class="docutils literal">global</tt> 声明。</p> +<pre class="code python literal-block"> +<span class="name">record</span> <span class="operator">=</span> <span class="punctuation">{</span><span class="literal string single">'s1'</span><span class="punctuation">:</span><span class="literal number integer">65</span><span class="punctuation">,</span> <span class="literal string single">'s2'</span><span class="punctuation">:</span><span class="literal number integer">60</span><span class="punctuation">}</span> + +<span class="keyword">def</span> <span class="name function">add_score</span><span class="punctuation">(</span><span class="name">student</span><span class="punctuation">,</span> <span class="name">score</span><span class="punctuation">):</span> + <span class="name">record</span><span class="punctuation">[</span><span class="name">student</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name">score</span> + + +<span class="keyword">print</span><span class="punctuation">(</span><span class="name">record</span><span class="punctuation">)</span> +<span class="name">add_score</span><span class="punctuation">(</span><span class="literal string single">'s3'</span><span class="punctuation">,</span> <span class="literal number integer">75</span><span class="punctuation">)</span> +<span class="keyword">print</span><span class="punctuation">(</span><span class="name">record</span><span class="punctuation">)</span> +</pre> +<p>练习: 定义一个函数 <tt class="docutils literal">empty_dict</tt> 清空字典 <tt class="docutils literal">record</tt>。 要求: 不能用 <tt class="docutils literal">return</tt> 语句。 提示: 可以用 <tt class="docutils literal">pop</tt> 方法, 或者直接给 <tt class="docutils literal">record</tt> 赋值 <tt class="docutils literal">{}</tt> 。</p> </div> -</blockquote> -<p>练习:给定一个字符串,数出每个单词出现的频率。</p> +<div class="section" id="id14"> +<h2><a class="toc-backref" href="#id41">函数执行顺序</a></h2> </div> </div> -<div class="section" id="id11"> -<h1><a class="toc-backref" href="#id33">参考</a></h1> +<div class="section" id="id15"> +<h1><a class="toc-backref" href="#id42">参考</a></h1> <ul class="simple"> <li>Think Python 2e – Green Tea Press. <a class="reference external" href="http://greenteapress.com/thinkpython2/thinkpython2.pdf">http://greenteapress.com/thinkpython2/thinkpython2.pdf</a>.</li> </ul> diff --git a/LectureNotesOnPython.rst b/LectureNotesOnPython.rst index 8505e95..80e9d5e 100644 --- a/LectureNotesOnPython.rst +++ b/LectureNotesOnPython.rst @@ -263,45 +263,45 @@ a = [1, 2] 可以原地修改a[0] = 2 数与格式化显示 ------------------------- -| x = 3.1415926 - -| print('%4.0f' % (x)) -| print('%4.1f' % (x)) -| print('%4.2f' % (x)) -| print('%4.3f' % (x)) -| print('%4.4f' % (x)) - - -| print('%6.0f' % (x)) -| print('%6.1f' % (x)) -| print('%6.2f' % (x)) -| print('%6.3f' % (x)) -| print('%6.4f' % (x)) - - -| print('%.0f' % (x)) -| print('%.1f' % (x)) -| print('%.2f' % (x)) -| print('%.3f' % (x)) -| print('%.4f' % (x)) -| print('%.5f' % (x)) -| print('%.6f' % (x)) -| print('%.7f' % (x)) -| print('%.8f' % (x)) -| print('%.9f' % (x)) -| print('%.15f' % (x)) -| print('%.16f' % (x)) -| print('%.17f' % (x)) -| print('%.18f' % (x)) - -| print('%4.f' % (x)) -| print('%5.f' % (x)) -| print('%6.f' % (x)) -| print('%7.f' % (x)) -| print('%8.f' % (x)) - -| print('%f' % (x)) - + | x = 3.1415926 + + | print('%4.0f' % (x)) + | print('%4.1f' % (x)) + | print('%4.2f' % (x)) + | print('%4.3f' % (x)) + | print('%4.4f' % (x)) + + + | print('%6.0f' % (x)) + | print('%6.1f' % (x)) + | print('%6.2f' % (x)) + | print('%6.3f' % (x)) + | print('%6.4f' % (x)) + + + | print('%.0f' % (x)) + | print('%.1f' % (x)) + | print('%.2f' % (x)) + | print('%.3f' % (x)) + | print('%.4f' % (x)) + | print('%.5f' % (x)) + | print('%.6f' % (x)) + | print('%.7f' % (x)) + | print('%.8f' % (x)) + | print('%.9f' % (x)) + | print('%.15f' % (x)) + | print('%.16f' % (x)) + | print('%.17f' % (x)) + | print('%.18f' % (x)) + + | print('%4.f' % (x)) + | print('%5.f' % (x)) + | print('%6.f' % (x)) + | print('%7.f' % (x)) + | print('%8.f' % (x)) + + | print('%f' % (x)) + 字符串(Strings) @@ -535,8 +535,8 @@ def f(x): list(map(f, [1,2]])) +filter方法,从几个值中选择符合条件的几个值。 -filter方法,从几个值中选择符合条件的几个值 | def f(x): | if x % 2 == 0: @@ -647,7 +647,45 @@ TDD - Test-driven Development 测试驱动开发。 My favourite。 刺激有挑战性。 帮助厘清需求。 帮助编写代码。 -推荐使用pytest。如何安装? 使用命令 ``pip install pytest`` +推荐使用pytest。如何安装? 使用命令 ``pip install pytest``。 + +在 ``test_cases.py`` 写如下测试用例。然后在命令行运行: ``python -m pytest test_cases.py`` 。 + +.. code:: python + + # Copyright (c) Hui Lan 2019 + + import random + import string + + def make_password(n): + ''' + Return a string of length n consisting of a combination of + letters, digits and special characters. Note that each password + must have at least one lower case letter, one upper case letter, + one digit and one special charater. Return an empty string if n + is less than 4. + ''' + + if n < 4: + return '' + + password = random.choice(string.ascii_lowercase) + \ + random.choice(string.ascii_uppercase) + \ + random.choice(string.digits) + \ + random.choice(string.punctuation) + \ + ''.join([random.choice(string.ascii_letters + string.digits + string.punctuation) for i in range(n-4)]) + + return ''.join(random.sample(password, n)) # shuffle password then return + + + + + if __name__ == '__main__': + for n in range(0,20): + pwd = make_password(n) + print(pwd) + @@ -697,39 +735,262 @@ item的顺序不可预测,不是按照创建时的顺序。 练习:给定一个字符串,数出每个字母出现的频率。 - | def histogram(s): - | ''' Cannot pass any test cases. ''' - | pass +.. code:: python + + def histogram(s): + ''' Cannot pass any test cases. ''' + pass - | def histogram(s): - | ''' Can pass the test case in which s is an empty string. ''' - | d = {} - | return d + def histogram(s): + ''' Can pass the test case in which s is an empty string. ''' + d = {} + return d - | def histogram(s): - | ''' Can pass the test cases in which all characters in s are unique. ''' - | d = {} - | for c in s: - | d[c] = 1 - | return d + def histogram(s): + ''' Can pass the test cases in which all characters in s are unique. ''' + d = {} + for c in s: + d[c] = 1 + return d - | def histogram(s): - | ''' Can pass all test cases. ''' - | d = {} - | for c in s: - | if c not in d: - | d[c] = 1 - | else: - | d[c] += 1 - | return d + def histogram(s): + ''' Can pass all test cases. ''' + d = {} + for c in s: + if c not in d: + d[c] = 1 + else: + d[c] += 1 + return d - | h = histogram('good') - | print(h) + h = histogram('good') + print(h) 练习:给定一个字符串,数出每个单词出现的频率。 +练习:给定一个新闻文本,数出每个单词出现的频率。考虑以下方面,(1)只考虑字典里有的单词。(2)单词周围如有标点符号,要先移除。 + +.. code:: python + + # Copyright (C) 2019 Hui Lan + # The following line fixes SyntaxError: Non-UTF-8 code starting with ... + # coding=utf8 + + def file2lst(fname): + ''' Return a list where each element is a word from fname. ''' + L = [] + f = open(fname) + for line in f: + line = line.strip() + lst = line.split() + for x in lst: + L.append(x) + f.close() + return L + + + def lst2dict(lst): + ''' Return a dictionary given list lst. Each key is an element in the lst. + The value is always 1.''' + d = {} + for w in lst: + d[w] = 1 + return d + + + import string + def remove_punctuation(s): + p = ',.:’“”' + string.punctuation + t = '' + for c in s: + if not c in p: + t += c + elif c == '’': # handle the case such as May’s + return t + return t + + def word_frequency(fname, english_dictionary): + ''' Return a dictionary where each key is a word both in the file fname and in + the dictionary english_dictionary, and the corresponding value is the frequency + of that word.''' + d = {} + L = file2lst(fname) + for x in L: + x = remove_punctuation(x.lower()) + if x in english_dictionary: + if not x in d: + d[x] = 1 + else: + d[x] += 1 + return d + + + def sort_by_value(d): + ''' Return a sorted list of tuples, each tuple containing a key and a value. + Note that the tuples are order in descending order of the value.''' + import operator + lst = sorted(d.items(), key=operator.itemgetter(1), reverse=True) + return lst + + + if __name__ == '__main__': + ed = lst2dict(file2lst('words.txt')) # from http://greenteapress.com/thinkpython2/code/words.txt + d = word_frequency('brexit-news.txt', ed) + lst = sort_by_value(d) + for x in lst: + print('%s (%d)' % (x[0], x[1])) + + + +key与value互换 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +注意到在原来的字典中一个value可能对应多个key的值。比如 ``d = {'a':1, 'b':2, 'c':2}`` 中,2就对应两个key,'b'与'c'。 + + +.. code:: python + + def inverse_dictionary(d): + d2 = {} + for k in d: + v = d[k] + if not v in d2: + d2[v] = [k] + else: + d2[v].append(k) + return d2 + + + + d = {'a':1, 'b':2, 'c':2} + d2 = inverse_dictionary(d) + print(d2) + + +练习: 用 ``inverse_dictionary`` 对上面 ``d = word_frequency('brexit-news.txt', ed)`` 产生的 ``d`` 进行转化。然后按照单词出现频率从高到低把所有单词都显示出来。每行显示一个频率内的所有单词。 + + +.. code:: python + + d2 = inverse_dictionary(d) + for k in sorted(d2.keys(), reverse=True): + print('%d %s' % (k, ' '.join(d2[k]))) + + +练习: 使用 ``setdefault`` 方法对上面的 ``inverse_dictionary`` 进行简化 (减少行数)。 + + +.. code:: python + + + + def inverse_dictionary(d): + d2 = {} + for k in d: + v = d[k] + d2.setdefault(v, []).append(k) + + return d2 + + + + +函数 +------ + +函数 ``unique_words`` 与 ``unique_words2`` 哪个运行速度快? + +.. code:: python + + def unique_words(lst): + d = {} + for x in lst: + d[x] = 1 + return sorted(d.keys()) + + def unique_words2(lst): + return sorted(list(set(lst))) + + + N = 10000000 + print(unique_words(['hello', 'world', 'am', 'he'] * N)) + print(unique_words2(['hello', 'world', 'am', 'he'] * N)) + + + +局部变量 +~~~~~~~~~~~~~~~~ + +在函数之内。函数执行结束,局部变量消失。 + + +全局变量 +~~~~~~~~~~~~~~~~ + +全局变量位于函数之外,模块之内。全局变量对所有模块内的函数可见(可读)。如果在函数内要对全局变量重新赋值,那么要先用 ``global`` 声明之 (declare)。 + + +.. code:: python + + verbose = True + + def example1(): + if verbose: + print('Running example1') + + def example2(): + verbose = False # a NEW local variable verbose + if verbose: + print('Running example2') + + def example3(): + global verbose # I am actually going to use the global variable verbose; don't create a local one. + verbose = False + if verbose: + print('Running example3') + + + + print(verbose) + example1() + + print(verbose) + example2() + example1() + + print(verbose) + example3() + example1() + + print(verbose) + + + +全局的列表与字典,如果只需改变其内容,而不是重新赋值,则不需要用 ``global`` 声明。 + + +.. code:: python + + record = {'s1':65, 's2':60} + + def add_score(student, score): + record[student] = score + + + print(record) + add_score('s3', 75) + print(record) + + +练习: 定义一个函数 ``empty_dict`` 清空字典 ``record``。 要求: 不能用 ``return`` 语句。 提示: 可以用 ``pop`` 方法, 或者直接给 ``record`` 赋值 ``{}`` 。 + + +函数执行顺序 +~~~~~~~~~~~~~~~~~~~~~~~~~ + + 参考 ------ |