<h2 id="译the-python-tutorialdata-structures">[译]The Python Tutorial#Data Structures</h2>
<h2 id="data-structures">5.1 Data Structures</h2>
<p>本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容。</p>
<h3 id="more-on-lists">5.1 More on Lists</h3>
<p>列表数据类型拥有更多方法,以下是列表对象的所有方法:</p>
<ul>
<li><p><code>list.append(x)</code><br>
在列表末尾添加新项,等同于<code>a[len(a):] = [x]</code></p></li>
<li><p><code>list.extend(iterable)</code><br>
添加可迭代对象中所有的项来扩展列表,等同于<code>a[len(a):] = iterable</code></p></li>
<li><p><code>list.insert(i, x)</code><br>
在指定位置插入项。第一个参数为元素索引,新的项会在这个索引之前插入,因此<code>a.insert(0, x)</code>会在列表最前面插入,<code>a.insert(len(a), x)</code>等同于<code>a.append(x)</code>。</p></li>
<li><p><code>list.remove(x)</code><br>
从列表中移除值为<code>x</code>的第一个项,若<code>x</code>不存在,方法抛出异常(<code>ValueError</code>异常)</p></li>
<li><p><code>list.pop([i])</code><br>
从列表中移除指定位置的项并返回。如果没有指定索引,<code>a.pop()</code>移除并返回列表中最后一个项。(方法签名中包裹<code>i</code>的方括号表示参数是可选的,而不是在这个位置写一个方括号。这种记号法在Python Library Reference中经常用到)</p></li>
<li><p><code>list.clear()</code><br>
移除列表中的所有项,等同于<code>del a[:]</code></p></li>
<li><p><code>list.index(x[, start[, end]])</code><br>
返回第一个值为<code>x</code>的项的基于0的索引,如果<code>x</code>不存在抛出<code>ValueError</code>异常。<br>
可选参数<em>start</em>和<em>end</em>被解释为切片记号法,用来将搜索限制在列表特定的子列表内。返回的索引是相对于完整列表索引,而不是相对于<em>start</em>参数的。</p></li>
<li><p><code>list.count(x)</code><br>
返回列表中<em>x</em>出现的次数</p></li>
<li><p><code>list.sort(key=None, reverse=False)</code><br>
对列表的所有项进行排序(参数用来自定义排序,参见<code>sorted()</code>获取更多信息)</p></li>
<li><p><code>list.reverse()</code><br>
反转列表元素</p></li>
<li><p><code>list.copy()</code><br>
返回列表的浅拷贝,等同于<code>a[:]</code></p></li>
</ul>
<p>以下是演示列表方法的例子:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> fruits <span class="op">=</span> [<span class="st">'orange'</span>, <span class="st">'apple'</span>, <span class="st">'pear'</span>, <span class="st">'banana'</span>, <span class="st">'kiwi'</span>, <span class="st">'apple'</span>, <span class="st">'banana'</span>]
<span class="op">>>></span> fruits.count(<span class="st">'apple'</span>)
<span class="dv">2</span>
<span class="op">>>></span> fruits.count(<span class="st">'tangerine'</span>)
<span class="dv">0</span>
<span class="op">>>></span> fruits.index(<span class="st">'banana'</span>)
<span class="dv">3</span>
<span class="op">>>></span> fruits.index(<span class="st">'banana'</span>, <span class="dv">4</span>) <span class="co"># Find next banana starting a position 4</span>
<span class="dv">6</span>
<span class="op">>>></span> fruits.reverse()
<span class="op">>>></span> fruits
[<span class="st">'banana'</span>, <span class="st">'apple'</span>, <span class="st">'kiwi'</span>, <span class="st">'banana'</span>, <span class="st">'pear'</span>, <span class="st">'apple'</span>, <span class="st">'orange'</span>]
<span class="op">>>></span> fruits.append(<span class="st">'grape'</span>)
<span class="op">>>></span> fruits
[<span class="st">'banana'</span>, <span class="st">'apple'</span>, <span class="st">'kiwi'</span>, <span class="st">'banana'</span>, <span class="st">'pear'</span>, <span class="st">'apple'</span>, <span class="st">'orange'</span>, <span class="st">'grape'</span>]
<span class="op">>>></span> fruits.sort()
<span class="op">>>></span> fruits
[<span class="st">'apple'</span>, <span class="st">'apple'</span>, <span class="st">'banana'</span>, <span class="st">'banana'</span>, <span class="st">'grape'</span>, <span class="st">'kiwi'</span>, <span class="st">'orange'</span>, <span class="st">'pear'</span>]
<span class="op">>>></span> fruits.pop()
<span class="co">'pear'</span></code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>诸如<code>insert, reverse</code>或者<code>sort</code>的这样,只改变了列表但是没有返回值打印,它们返回默认的<code>None</code><sup>[1]</sup>。这是Python可变数据结构适用的设计原则。</p>
<h4 id="using-lists-as-stacks">5.1.1 Using Lists as Stacks</h4>
<p>列表方法使得将其用作栈非常容易,栈中最后一个加入的元素第一个被释放(后进先出)。使用<code>append()</code>方法添加元素到栈顶,使用不带参数的<code>pop()</code>将栈顶元素出栈。示例:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> stack <span class="op">=</span> [<span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>]
<span class="op">>>></span> stack.append(<span class="dv">6</span>)
<span class="op">>>></span> stack.append(<span class="dv">7</span>)
<span class="op">>>></span> stack
[<span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>]
<span class="op">>>></span> stack.pop()
<span class="dv">7</span>
<span class="op">>>></span> stack
[<span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>]
<span class="op">>>></span> stack.pop()
<span class="dv">6</span>
<span class="op">>>></span> stack.pop()
<span class="dv">5</span>
<span class="op">>>></span> stack
[<span class="dv">3</span>, <span class="dv">4</span>]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<h4 id="using-lists-as-queues">5.1.2 Using Lists as Queues</h4>
<p>将列表用作队列也是可能的,队列中先添加的元素先释放(先进先出);然而,这样用列表效率非常不高。因为在列表末尾添加和取出元素很快,但是在列表开头插入或者删除元素很慢(因为不得不将其他所有元素位移一位)。</p>
<p>经过特殊设计的<code>collections.deque</code>在首尾两端添加和删除元素都很快,可以使用它来实现队列。示例:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> <span class="im">from</span> collections <span class="im">import</span> deque
<span class="op">>>></span> queue <span class="op">=</span> deque([<span class="st">"Eric"</span>, <span class="st">"John"</span>, <span class="st">"Michael"</span>])
<span class="op">>>></span> queue.append(<span class="st">"Terry"</span>) <span class="co"># Terry arrives</span>
<span class="op">>>></span> queue.append(<span class="st">"Graham"</span>) <span class="co"># Graham arrives</span>
<span class="op">>>></span> queue.popleft() <span class="co"># The first to arrive now leaves</span>
<span class="co">'Eric'</span>
<span class="op">>>></span> queue.popleft() <span class="co"># The second to arrive now leaves</span>
<span class="co">'John'</span>
<span class="op">>>></span> queue <span class="co"># Remaining queue in order of arrival</span>
deque([<span class="st">'Michael'</span>, <span class="st">'Terry'</span>, <span class="st">'Graham'</span>])</code></pre><div class="contentsignin">登录后复制</div></div></div>
<h4 id="list-comprehensions">5.1.3 List Comprehensions</h4>
<p>列表推导式为创建列表提供了简洁方式。一般的应用方式是:创建新列表,列表元素是对其他序列或者可迭代对象操作的结果;或者创建元素满足特定条件的子序列。</p>
<p>假如希望创建一个平方列表:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> squares <span class="op">=</span> []
<span class="op">>>></span> <span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="dv">10</span>):
... squares.append(x<span class="op">**</span><span class="dv">2</span>)
...
<span class="op">>>></span> squares
[<span class="dv">0</span>, <span class="dv">1</span>, <span class="dv">4</span>, <span class="dv">9</span>, <span class="dv">16</span>, <span class="dv">25</span>, <span class="dv">36</span>, <span class="dv">49</span>, <span class="dv">64</span>, <span class="dv">81</span>]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>注意以上创建(或者重写)了名为<code>x</code>的变量,该变量在循环结束之后仍然存在。使用以下方法可创建没有任何副作用的平方列表:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python">squares <span class="op">=</span> <span class="bu">list</span>(<span class="bu">map</span>(<span class="kw">lambda</span> x: x<span class="op">**</span><span class="dv">2</span>, <span class="bu">range</span>(<span class="dv">10</span>)))</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>或者,等用于:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python">squares <span class="op">=</span> [x<span class="op">**</span><span class="dv">2</span> <span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="dv">10</span>)]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>这种方式更加简洁和易读。</p>
<p>跟随着<code>for</code>子句,紧接零个或者多个<code>for</code>子句或者<code>if</code>子句的表达式再加上中括号,构成了列表推导式。其返回结果是一个新的列表,列表的元素是表达式中<code>for</code>和<code>if</code>子句的计算结果。例如,以下列表推导式组合两个列表中不相等的元素:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> [(x, y) <span class="cf">for</span> x <span class="op">in</span> [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>] <span class="cf">for</span> y <span class="op">in</span> [<span class="dv">3</span>,<span class="dv">1</span>,<span class="dv">4</span>] <span class="cf">if</span> x <span class="op">!=</span> y]
[(<span class="dv">1</span>, <span class="dv">3</span>), (<span class="dv">1</span>, <span class="dv">4</span>), (<span class="dv">2</span>, <span class="dv">3</span>), (<span class="dv">2</span>, <span class="dv">1</span>), (<span class="dv">2</span>, <span class="dv">4</span>), (<span class="dv">3</span>, <span class="dv">1</span>), (<span class="dv">3</span>, <span class="dv">4</span>)]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>等价于:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> combs <span class="op">=</span> []
<span class="op">>>></span> <span class="cf">for</span> x <span class="op">in</span> [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>]:
... <span class="cf">for</span> y <span class="op">in</span> [<span class="dv">3</span>,<span class="dv">1</span>,<span class="dv">4</span>]:
... <span class="cf">if</span> x <span class="op">!=</span> y:
... combs.append((x, y))
...
<span class="op">>>></span> combs
[(<span class="dv">1</span>, <span class="dv">3</span>), (<span class="dv">1</span>, <span class="dv">4</span>), (<span class="dv">2</span>, <span class="dv">3</span>), (<span class="dv">2</span>, <span class="dv">1</span>), (<span class="dv">2</span>, <span class="dv">4</span>), (<span class="dv">3</span>, <span class="dv">1</span>), (<span class="dv">3</span>, <span class="dv">4</span>)]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>注意上面两个代码段中<code>for</code>和<code>if</code>语句的顺序是相同的。</p>
<p>如果表达式是一个元组(如上所示的(x, y)),必须将其加上括号。</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> vec <span class="op">=</span> [<span class="op">-</span><span class="dv">4</span>, <span class="op">-</span><span class="dv">2</span>, <span class="dv">0</span>, <span class="dv">2</span>, <span class="dv">4</span>]
<span class="op">>>></span> <span class="co"># create a new list with the values doubled</span>
<span class="op">>>></span> [x<span class="op">*</span><span class="dv">2</span> <span class="cf">for</span> x <span class="op">in</span> vec]
[<span class="op">-</span><span class="dv">8</span>, <span class="op">-</span><span class="dv">4</span>, <span class="dv">0</span>, <span class="dv">4</span>, <span class="dv">8</span>]
<span class="op">>>></span> <span class="co"># filter the list to exclude negative numbers</span>
<span class="op">>>></span> [x <span class="cf">for</span> x <span class="op">in</span> vec <span class="cf">if</span> x <span class="op">>=</span> <span class="dv">0</span>]
[<span class="dv">0</span>, <span class="dv">2</span>, <span class="dv">4</span>]
<span class="op">>>></span> <span class="co"># apply a function to all the elements</span>
<span class="op">>>></span> [<span class="bu">abs</span>(x) <span class="cf">for</span> x <span class="op">in</span> vec]
[<span class="dv">4</span>, <span class="dv">2</span>, <span class="dv">0</span>, <span class="dv">2</span>, <span class="dv">4</span>]
<span class="op">>>></span> <span class="co"># call a method on each element</span>
<span class="op">>>></span> freshfruit <span class="op">=</span> [<span class="st">' banana'</span>, <span class="st">' loganberry '</span>, <span class="st">'passion fruit '</span>]
<span class="op">>>></span> [weapon.strip() <span class="cf">for</span> weapon <span class="op">in</span> freshfruit]
[<span class="st">'banana'</span>, <span class="st">'loganberry'</span>, <span class="st">'passion fruit'</span>]
<span class="op">>>></span> <span class="co"># create a list of 2-tuples like (number, square)</span>
<span class="op">>>></span> [(x, x<span class="op">**</span><span class="dv">2</span>) <span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="dv">6</span>)]
[(<span class="dv">0</span>, <span class="dv">0</span>), (<span class="dv">1</span>, <span class="dv">1</span>), (<span class="dv">2</span>, <span class="dv">4</span>), (<span class="dv">3</span>, <span class="dv">9</span>), (<span class="dv">4</span>, <span class="dv">16</span>), (<span class="dv">5</span>, <span class="dv">25</span>)]
<span class="op">>>></span> <span class="co"># the tuple must be parenthesized, otherwise an error is raised</span>
<span class="op">>>></span> [x, x<span class="op">**</span><span class="dv">2</span> <span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="dv">6</span>)]
File <span class="st">"<stdin>"</span>, line <span class="dv">1</span>, <span class="op">in</span> <span class="op"><</span>module<span class="op">></span>
[x, x<span class="op">**</span><span class="dv">2</span> <span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="dv">6</span>)]
<span class="op">^</span>
<span class="pp">SyntaxError</span>: invalid syntax
<span class="op">>>></span> <span class="co"># flatten a list using a listcomp with two 'for'</span>
<span class="op">>>></span> vec <span class="op">=</span> [[<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>], [<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">6</span>], [<span class="dv">7</span>,<span class="dv">8</span>,<span class="dv">9</span>]]
<span class="op">>>></span> [num <span class="cf">for</span> elem <span class="op">in</span> vec <span class="cf">for</span> num <span class="op">in</span> elem]
[<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>, <span class="dv">8</span>, <span class="dv">9</span>]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>列表推导式可以包含复杂的表达式甚至嵌套函数:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> <span class="im">from</span> math <span class="im">import</span> pi
<span class="op">>>></span> [<span class="bu">str</span>(<span class="bu">round</span>(pi, i)) <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">6</span>)]
[<span class="st">'3.1'</span>, <span class="st">'3.14'</span>, <span class="st">'3.142'</span>, <span class="st">'3.1416'</span>, <span class="st">'3.14159'</span>]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<h4 id="nested-list-comprehensions">5.1.4 Nested List Comprehensions</h4>
<p>列表推导式开头的表达式可以是任意表达式,包括另一个列表推导式。</p>
<p>考虑以下示例,一个包含3个长度为4的列表的列表实现了3x4的矩阵:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python">matrix <span class="op">=</span> [
[<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>],
[<span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>, <span class="dv">8</span>],
[<span class="dv">9</span>, <span class="dv">10</span>, <span class="dv">11</span>, <span class="dv">12</span>],
]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>以下列表推导式反转行列:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> [[row[i] <span class="cf">for</span> row <span class="op">in</span> matrix] <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">4</span>)]
[[<span class="dv">1</span>, <span class="dv">5</span>, <span class="dv">9</span>], [<span class="dv">2</span>, <span class="dv">6</span>, <span class="dv">10</span>], [<span class="dv">3</span>, <span class="dv">7</span>, <span class="dv">11</span>], [<span class="dv">4</span>, <span class="dv">8</span>, <span class="dv">12</span>]]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>前面的章节提到,嵌套的列表推导式是在其后跟随的<code>for</code>的上下文中求值的,因此这个示例等同于:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> transposed <span class="op">=</span> []
<span class="op">>>></span> <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">4</span>):
... transposed.append([row[i] <span class="cf">for</span> row <span class="op">in</span> matrix])
...
<span class="op">>>></span> transposed
[[<span class="dv">1</span>, <span class="dv">5</span>, <span class="dv">9</span>], [<span class="dv">2</span>, <span class="dv">6</span>, <span class="dv">10</span>], [<span class="dv">3</span>, <span class="dv">7</span>, <span class="dv">11</span>], [<span class="dv">4</span>, <span class="dv">8</span>, <span class="dv">12</span>]]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>依次等同于:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> transposed <span class="op">=</span> []
<span class="op">>>></span> <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">4</span>):
... <span class="co"># the following 3 lines implement the nested listcomp</span>
... transposed_row <span class="op">=</span> []
... <span class="cf">for</span> row <span class="op">in</span> matrix:
... transposed_row.append(row[i])
... transposed.append(transposed_row)
<span class="op">>>></span> transposed
[[<span class="dv">1</span>, <span class="dv">5</span>, <span class="dv">9</span>], [<span class="dv">2</span>, <span class="dv">6</span>, <span class="dv">10</span>], [<span class="dv">3</span>, <span class="dv">7</span>, <span class="dv">11</span>], [<span class="dv">4</span>, <span class="dv">8</span>, <span class="dv">12</span>]]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>在实践中,应该选择built-in函数来复合流程语句。在以上的用例中<code>zip()</code>函数更有用:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> <span class="bu">list</span>(<span class="bu">zip</span>(<span class="op">*</span>matrix))
[(<span class="dv">1</span>, <span class="dv">5</span>, <span class="dv">9</span>), (<span class="dv">2</span>, <span class="dv">6</span>, <span class="dv">10</span>), (<span class="dv">3</span>, <span class="dv">7</span>, <span class="dv">11</span>), (<span class="dv">4</span>, <span class="dv">8</span>, <span class="dv">12</span>)]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>参见 Unpacking Argument Lists了解关于上面<code>*</code>使用的更多详细信息。</p>
<h3 id="the-del-statement">5.2 The del statement</h3>
<p>在提供列表索引而不是值的情况下,有一种方法可以移除列表中的元素:<code>del</code>语句。这种方式与返回值的<code>pop()</code>方法不同。<code>del</code>语句也可以用来移除部分列表或者清除整个列表(之前使用将空的列表赋值给列表片段的方式实现)。示例:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> a <span class="op">=</span> [<span class="op">-</span><span class="dv">1</span>, <span class="dv">1</span>, <span class="fl">66.25</span>, <span class="dv">333</span>, <span class="dv">333</span>, <span class="fl">1234.5</span>]
<span class="op">>>></span> <span class="kw">del</span> a[<span class="dv">0</span>]
<span class="op">>>></span> a
[<span class="dv">1</span>, <span class="fl">66.25</span>, <span class="dv">333</span>, <span class="dv">333</span>, <span class="fl">1234.5</span>]
<span class="op">>>></span> <span class="kw">del</span> a[<span class="dv">2</span>:<span class="dv">4</span>]
<span class="op">>>></span> a
[<span class="dv">1</span>, <span class="fl">66.25</span>, <span class="fl">1234.5</span>]
<span class="op">>>></span> <span class="kw">del</span> a[:]
<span class="op">>>></span> a
[]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p><code>del</code>也可以用来删除整个变量:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> <span class="kw">del</span> a</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>此后引用名字<code>a</code>会抛出异常(至少在其他值赋值给名字<code>a</code>之前)。接下来会有更多<code>del</code>的使用</p>
<h3 id="tuples-and-sequences">5.3 Tuples and Sequences</h3>
<p>列表和字符串有很多常用属性,比如索引和切片操作。它们是<em>序列</em>数据类型(参见 Sequence Types - list, tuple, range)的两种。Python是一种不断进化的语言,其他的序列类型也可以加入。<em>元组</em>是另一种标准的序列数据类型。</p>
<p>元组包含若干由逗号分隔的值,示例:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> t <span class="op">=</span> <span class="dv">12345</span>, <span class="dv">54321</span>, <span class="st">'hello!'</span>
<span class="op">>>></span> t[<span class="dv">0</span>]
<span class="dv">12345</span>
<span class="op">>>></span> t
(<span class="dv">12345</span>, <span class="dv">54321</span>, <span class="st">'hello!'</span>)
<span class="op">>>></span> <span class="co"># Tuples may be nested:</span>
... u <span class="op">=</span> t, (<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>)
<span class="op">>>></span> u
((<span class="dv">12345</span>, <span class="dv">54321</span>, <span class="st">'hello!'</span>), (<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>))
<span class="op">>>></span> <span class="co"># Tuples are immutable:</span>
... t[<span class="dv">0</span>] <span class="op">=</span> <span class="dv">88888</span>
Traceback (most recent call last):
File <span class="st">"<stdin>"</span>, line <span class="dv">1</span>, <span class="op">in</span> <span class="op"><</span>module<span class="op">></span>
<span class="pp">TypeError</span>: <span class="st">'tuple'</span> <span class="bu">object</span> does <span class="op">not</span> support item assignment
<span class="op">>>></span> <span class="co"># but they can contain mutable objects:</span>
... v <span class="op">=</span> ([<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>], [<span class="dv">3</span>, <span class="dv">2</span>, <span class="dv">1</span>])
<span class="op">>>></span> v
([<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>], [<span class="dv">3</span>, <span class="dv">2</span>, <span class="dv">1</span>])</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>可见,输出的元组总是放在圆括号中,以便于嵌套的元组可以被正确解析;虽然圆括号总是必须的(如果元组是其他更大表达式的一部分),但是在输入元组的时候可以选择使用圆括号。不能对元组的单个项赋值,但是可以创建包含如列表的可变对象的元组。</p>
<p>虽然元组和列表有些相似,但是他们通常以不同的目的,用于不同的场景。元组是不可变的,通常包含不同类型的元素,可以通过拆包操作(参见后续章节)或者索引(或者当元组是命名元组时,甚至可以通过属性来访问)来访问。列表是可变的,通常其元素也是不同类型的,可以通过对列表的迭代访问元素。</p>
<p>构建包含零个或者1个项的元组比较特殊:一种额外的奇怪语法可以适用于这种情况。空元组由一对空的圆括号创建;一个元素的元组由一个跟着逗号的值创建(在圆括号中放入单个值是不够的。译注:这种情况:(1)表示整数而不是元组,使用(1, )表示元组也是可行的)。丑陋但是有效。示例:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> empty <span class="op">=</span> ()
<span class="op">>>></span> singleton <span class="op">=</span> <span class="st">'hello'</span>, <span class="co"># <-- note trailing comma</span>
<span class="op">>>></span> <span class="bu">len</span>(empty)
<span class="dv">0</span>
<span class="op">>>></span> <span class="bu">len</span>(singleton)
<span class="dv">1</span>
<span class="op">>>></span> singleton
(<span class="st">'hello'</span>,)</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>语句<code>t = 12345, 54321, 'hello!'</code>是封装元组的一个示例:值<code>12345, 54321</code>和<code>hello!</code>被封装到了一个元组中。逆向操作也是可行的:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> x, y, z <span class="op">=</span> t</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>非常恰当地称之为<em>序列解包</em>,适用于任何在等号右边的序列(译注:等号右操作数)。序列解包要求等号左边待赋值的变量数量与序列包含元素数目相同。注意多重赋值只是封装元组和序列解包的结合(译注:多重赋值:<code>i, j = 1, 2</code>)</p>
<h3 id="set">5.4 Set</h3>
<p>Python也包含实现了<em>集合</em>的数据类型。集合是无序不重复的元素集。基本功能包括成员关系测试和重复实体消除。集合对象也支持并集,交集,差集以及对称差集等数学操作。</p>
<p>可以使用花括号和<code>set()</code>函数创建集合。谨记:创建空集合必须使用<code>set</code>函数,不能使用<code>{}</code>,后者用于创建空字典。</p>
<p>以下是简单示范:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> basket <span class="op">=</span> {<span class="st">'apple'</span>, <span class="st">'orange'</span>, <span class="st">'apple'</span>, <span class="st">'pear'</span>, <span class="st">'orange'</span>, <span class="st">'banana'</span>}
<span class="op">>>></span> <span class="bu">print</span>(basket) <span class="co"># show that duplicates have been removed</span>
{<span class="st">'orange'</span>, <span class="st">'banana'</span>, <span class="st">'pear'</span>, <span class="st">'apple'</span>}
<span class="op">>>></span> <span class="st">'orange'</span> <span class="op">in</span> basket <span class="co"># fast membership testing</span>
<span class="va">True</span>
<span class="op">>>></span> <span class="st">'crabgrass'</span> <span class="op">in</span> basket
<span class="va">False</span>
<span class="op">>>></span> <span class="co"># Demonstrate set operations on unique letters from two words</span>
...
<span class="op">>>></span> a <span class="op">=</span> <span class="bu">set</span>(<span class="st">'abracadabra'</span>)
<span class="op">>>></span> b <span class="op">=</span> <span class="bu">set</span>(<span class="st">'alacazam'</span>)
<span class="op">>>></span> a <span class="co"># unique letters in a</span>
{<span class="st">'a'</span>, <span class="st">'r'</span>, <span class="st">'b'</span>, <span class="st">'c'</span>, <span class="st">'d'</span>}
<span class="op">>>></span> a <span class="op">-</span> b <span class="co"># letters in a but not in b</span>
{<span class="st">'r'</span>, <span class="st">'d'</span>, <span class="st">'b'</span>}
<span class="op">>>></span> a <span class="op">|</span> b <span class="co"># letters in a or b or both</span>
{<span class="st">'a'</span>, <span class="st">'c'</span>, <span class="st">'r'</span>, <span class="st">'d'</span>, <span class="st">'b'</span>, <span class="st">'m'</span>, <span class="st">'z'</span>, <span class="st">'l'</span>}
<span class="op">>>></span> a <span class="op">&</span> b <span class="co"># letters in both a and b</span>
{<span class="st">'a'</span>, <span class="st">'c'</span>}
<span class="op">>>></span> a <span class="op">^</span> b <span class="co"># letters in a or b but not both</span>
{<span class="st">'r'</span>, <span class="st">'d'</span>, <span class="st">'b'</span>, <span class="st">'m'</span>, <span class="st">'z'</span>, <span class="st">'l'</span>}</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>与列表推导式相同,Python也支持集合推导式:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> a <span class="op">=</span> {x <span class="cf">for</span> x <span class="op">in</span> <span class="st">'abracadabra'</span> <span class="cf">if</span> x <span class="op">not</span> <span class="op">in</span> <span class="st">'abc'</span>}
<span class="op">>>></span> a
{<span class="st">'r'</span>, <span class="st">'d'</span>}</code></pre><div class="contentsignin">登录后复制</div></div></div>
<h3 id="dictionaries">5.5 Dictionaries</h3>
<p>另一个内嵌入Python中的数据结构是<em>字典</em>(参见 Mapping Types - dict)。字典在其他一些语言中被称为<em>“联合存储”</em>或者<em>“联合数组”</em>。与序列不同,序列以一系列数字作索引,字典以<em>键</em>作索引,键可以是任何不可变类型;通常使用字符串和数字作为键。只包含字符串,数字或者其他元组的元组也可以作为键;直接或者间接包含可变对象的元组不能作为键。因为列表可以使用索引赋值,切片赋值或者<code>append()</code>以及<code>extend()</code>等方法改变自身,所以列表不能作为键。</p>
<p>最好的理解字典的方式是将其认为是键值对的无序集合,同一集合中键唯一。一对花括号创建空字典:<code>{}</code>。在花括号中放置由逗号分隔键值对列表可以为字典添加初始键值对;这也是字典输出的格式。</p>
<p>字典提供的主要操作是:使用键存储值以及取值。可以使用<code>del</code>删除一个键值对。如果使用已经存在的键来存储值,那么与键关联的旧值会被重写。使用不存在的键来取值会抛出异常。</p>
<p>在字典上执行<code>list(d.keys())</code>返回字典所有键的无序列表(使用<code>sorted(d.keys())</code>使其有序)<sup>[2]</sup>。使用关键字<code>in</code>检查键在字典中是否存在。</p>
<p>以下是使用字典的示例:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> tel <span class="op">=</span> {<span class="st">'jack'</span>: <span class="dv">4098</span>, <span class="st">'sape'</span>: <span class="dv">4139</span>}
<span class="op">>>></span> tel[<span class="st">'guido'</span>] <span class="op">=</span> <span class="dv">4127</span>
<span class="op">>>></span> tel
{<span class="st">'sape'</span>: <span class="dv">4139</span>, <span class="st">'guido'</span>: <span class="dv">4127</span>, <span class="st">'jack'</span>: <span class="dv">4098</span>}
<span class="op">>>></span> tel[<span class="st">'jack'</span>]
<span class="dv">4098</span>
<span class="op">>>></span> <span class="kw">del</span> tel[<span class="st">'sape'</span>]
<span class="op">>>></span> tel[<span class="st">'irv'</span>] <span class="op">=</span> <span class="dv">4127</span>
<span class="op">>>></span> tel
{<span class="st">'guido'</span>: <span class="dv">4127</span>, <span class="st">'irv'</span>: <span class="dv">4127</span>, <span class="st">'jack'</span>: <span class="dv">4098</span>}
<span class="op">>>></span> <span class="bu">list</span>(tel.keys())
[<span class="st">'irv'</span>, <span class="st">'guido'</span>, <span class="st">'jack'</span>]
<span class="op">>>></span> <span class="bu">sorted</span>(tel.keys())
[<span class="st">'guido'</span>, <span class="st">'irv'</span>, <span class="st">'jack'</span>]
<span class="op">>>></span> <span class="st">'guido'</span> <span class="op">in</span> tel
<span class="va">True</span>
<span class="op">>>></span> <span class="st">'jack'</span> <span class="op">not</span> <span class="op">in</span> tel
<span class="va">False</span></code></pre><div class="contentsignin">登录后复制</div></div></div>
<p><code>dict()</code>构造器直接使用键值对序列构造字典:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> <span class="bu">dict</span>([(<span class="st">'sape'</span>, <span class="dv">4139</span>), (<span class="st">'guido'</span>, <span class="dv">4127</span>), (<span class="st">'jack'</span>, <span class="dv">4098</span>)])
{<span class="st">'sape'</span>: <span class="dv">4139</span>, <span class="st">'jack'</span>: <span class="dv">4098</span>, <span class="st">'guido'</span>: <span class="dv">4127</span>}</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>此外,字典推导式可以从任意键值表达式中创建字典:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> {x: x<span class="op">**</span><span class="dv">2</span> <span class="cf">for</span> x <span class="op">in</span> (<span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">6</span>)}
{<span class="dv">2</span>: <span class="dv">4</span>, <span class="dv">4</span>: <span class="dv">16</span>, <span class="dv">6</span>: <span class="dv">36</span>}</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>当键是简单的字符串时,可以使用关键字参数来指定键值对:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> <span class="bu">dict</span>(sape<span class="op">=</span><span class="dv">4139</span>, guido<span class="op">=</span><span class="dv">4127</span>, jack<span class="op">=</span><span class="dv">4098</span>)
{<span class="st">'sape'</span>: <span class="dv">4139</span>, <span class="st">'jack'</span>: <span class="dv">4098</span>, <span class="st">'guido'</span>: <span class="dv">4127</span>}</code></pre><div class="contentsignin">登录后复制</div></div></div>
<h3 id="looping-techniques">5.6 Looping Techniques</h3>
<p>遍历字典时,使用<code>items()</code>方法可以同时检索键及其对应的值。</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> knights <span class="op">=</span> {<span class="st">'gallahad'</span>: <span class="st">'the pure'</span>, <span class="st">'robin'</span>: <span class="st">'the brave'</span>}
<span class="op">>>></span> <span class="cf">for</span> k, v <span class="op">in</span> knights.items():
... <span class="bu">print</span>(k, v)
...
gallahad the pure
robin the brave</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>遍历序列时,使用<code>enumerate()</code>函数可以同时检索位置索引及其对应的值:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> <span class="cf">for</span> i, v <span class="op">in</span> <span class="bu">enumerate</span>([<span class="st">'tic'</span>, <span class="st">'tac'</span>, <span class="st">'toe'</span>]):
... <span class="bu">print</span>(i, v)
...
<span class="dv">0</span> tic
<span class="dv">1</span> tac
<span class="dv">2</span> toe</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>同时遍历两个或者更多序列时,使用<code>zip()</code>函数可以将元素组成对:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> questions <span class="op">=</span> [<span class="st">'name'</span>, <span class="st">'quest'</span>, <span class="st">'favorite color'</span>]
<span class="op">>>></span> answers <span class="op">=</span> [<span class="st">'lancelot'</span>, <span class="st">'the holy grail'</span>, <span class="st">'blue'</span>]
<span class="op">>>></span> <span class="cf">for</span> q, a <span class="op">in</span> <span class="bu">zip</span>(questions, answers):
... <span class="bu">print</span>(<span class="st">'What is your </span><span class="sc">{0}</span><span class="st">? It is </span><span class="sc">{1}</span><span class="st">.'</span>.<span class="bu">format</span>(q, a))
...
What <span class="op">is</span> your name? It <span class="op">is</span> lancelot.
What <span class="op">is</span> your quest? It <span class="op">is</span> the holy grail.
What <span class="op">is</span> your favorite color? It <span class="op">is</span> blue.</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>需要逆序遍历序列时,首先指定一个正向的序列,然后调用<code>reversed()</code>函数:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> <span class="cf">for</span> i <span class="op">in</span> <span class="bu">reversed</span>(<span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">10</span>, <span class="dv">2</span>)):
... <span class="bu">print</span>(i)
...
<span class="dv">9</span>
<span class="dv">7</span>
<span class="dv">5</span>
<span class="dv">3</span>
<span class="dv">1</span></code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>需要以特定顺序遍历序列时,使用<code>sorted()</code>函数返回新的有序序列,原序列不会改动:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> basket <span class="op">=</span> [<span class="st">'apple'</span>, <span class="st">'orange'</span>, <span class="st">'apple'</span>, <span class="st">'pear'</span>, <span class="st">'orange'</span>, <span class="st">'banana'</span>]
<span class="op">>>></span> <span class="cf">for</span> f <span class="op">in</span> <span class="bu">sorted</span>(<span class="bu">set</span>(basket)):
... <span class="bu">print</span>(f)
...
apple
banana
orange
pear</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>有时需要在遍历序列的同时修改序列,创建新的替代序列更加简单并且安全:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> <span class="im">import</span> math
<span class="op">>>></span> raw_data <span class="op">=</span> [<span class="fl">56.2</span>, <span class="bu">float</span>(<span class="st">'NaN'</span>), <span class="fl">51.7</span>, <span class="fl">55.3</span>, <span class="fl">52.5</span>, <span class="bu">float</span>(<span class="st">'NaN'</span>), <span class="fl">47.8</span>]
<span class="op">>>></span> filtered_data <span class="op">=</span> []
<span class="op">>>></span> <span class="cf">for</span> value <span class="op">in</span> raw_data:
... <span class="cf">if</span> <span class="op">not</span> math.isnan(value):
... filtered_data.append(value)
...
<span class="op">>>></span> filtered_data
[<span class="fl">56.2</span>, <span class="fl">51.7</span>, <span class="fl">55.3</span>, <span class="fl">52.5</span>, <span class="fl">47.8</span>]</code></pre><div class="contentsignin">登录后复制</div></div></div>
<h3 id="more-on-conditions">5.7 More on Conditions</h3>
<p><code>while</code>和<code>if</code>语句中使用的条件可以包含任意操作符,不仅仅是比较运算符。</p>
<p>比较运算符<code>in</code>和<code>not in</code>检查指定值在序列中是否存在(不存在)。操作符<code>is</code>和<code>is not</code>比较两个对象是否真正相同(内存地址比较);这两个操作符只对像列表那样的可变对象重要。所有的比较运算符拥有相同的优先级,并且都低于数字运算符。</p>
<p>比较运算符可以链接起来。例如<code>a < b == c</code>测试<code>b</code>是否大于<code>a</code>同时<code>b</code>等于<code>c</code>(译注:同其他高级语言的:<code>a < b and b == c</code>)</p>
<p>比较运算符可以结合布尔运算符<code>and</code>和<code>or</code>使用,比较的结果(或者其他任何布尔表达式)可以使用<code>not</code>来作否定。<code>and</code>,<code>or</code>和<code>not</code>优先级比比较运算符低;其中<code>not</code>的优先级最高而<code>or</code>优先级最低,因此<code>A and not B or C</code>等同于<code>(A and (not B)) or C</code>。一如既往,可以使用圆括号表述想要的优先级顺序。</p>
<p>布尔运算符<code>and</code>和<code>or</code>号称<em>短路</em>运算符:它们的参数从左向右求值,一旦结果确定,求值过程就会停止。例如,如果<code>A</code>和<code>C</code>是真,<code>B</code>是假,<code>A and B and C</code>不会对表达式<code>C</code>求值(译注:<code>A and B</code>为假,已经确定了整个表达式<code>A and B and C</code>的值为假,表达式<code>C</code>的值对结果不会造成影响,因此不会对其求值)。当用作一般值而不是布尔值时,短路操作的返回值是最后一个求值的参数。</p>
<p>可以将比较运算或者布尔表达式赋值给变量:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">>>></span> string1, string2, string3 <span class="op">=</span> <span class="st">''</span>, <span class="st">'Trondheim'</span>, <span class="st">'Hammer Dance'</span>
<span class="op">>>></span> non_null <span class="op">=</span> string1 <span class="op">or</span> string2 <span class="op">or</span> string3
<span class="op">>>></span> non_null
<span class="co">'Trondheim'</span></code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>注意在Python中,赋值操作不能像C语言一样在表达式内发生。C程序员也许会抱怨,但是这避免了C程序中遇到的一个普遍问题:当想要表示<code>==</code>时候可能误用了<code>=</code>。</p>
<h3 id="comparing-sequences-and-other-types">5.8 Comparing Sequences and Other Types</h3>
<p>相同序列类型之间的序列对象可以相互比较。比较使用字典序:首先比较两个序列的第一项,如果它们不同,比较运算的结果就可确定了;如果它们不同,比较两个序列中的下一个项,以此类推,直到其中一个序列耗尽。如果被比较的两个项是同一类型的,那么使用字典序递归比较。如果两个序列的所有项都是相等的,那么他们相等。如果其中一个序列是另一个序列的子序列,那么短的一个序列较小。字符串的字典序使用Unicode代码点数字排序单个字符。</p>
<p>以下是相同类型的序列对象之间的比较示例:</p>
<div class="sourceCode"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="sourceCode python"><code class="sourceCode python">(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>) <span class="op"><</span> (<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">4</span>)
[<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>] <span class="op"><</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">4</span>]
<span class="co">'ABC'</span> <span class="op"><</span> <span class="st">'C'</span> <span class="op"><</span> <span class="st">'Pascal'</span> <span class="op"><</span> <span class="st">'Python'</span>
(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>) <span class="op"><</span> (<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">4</span>)
(<span class="dv">1</span>, <span class="dv">2</span>) <span class="op"><</span> (<span class="dv">1</span>, <span class="dv">2</span>, <span class="op">-</span><span class="dv">1</span>)
(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>) <span class="op">==</span> (<span class="fl">1.0</span>, <span class="fl">2.0</span>, <span class="fl">3.0</span>)
(<span class="dv">1</span>, <span class="dv">2</span>, (<span class="st">'aa'</span>, <span class="st">'ab'</span>)) <span class="op"><</span> (<span class="dv">1</span>, <span class="dv">2</span>, (<span class="st">'abc'</span>, <span class="st">'a'</span>), <span class="dv">4</span>)</code></pre><div class="contentsignin">登录后复制</div></div></div>
<p>注意当不同类型对象之间有合适的比较方式时,使用<code><</code>或者<code>></code>比较不同类型的对象是合法的。例如,混合数字类型之间的比较是根据其数字上的值,0等于0.0。否则,解释器会抛出<code>TypeException</code>异常,而不是随意提供结果</p>
<h3 id="footnotes">Footnotes</h3>
<p><span id="jump1">[1] </span> 其他语言可能返回改变后的对象,从而允许方法链接,如:<code>d->insert("a")>remove("b")->sort();</code><br><span id="jump2">[2] </span> 调用<code>d.keys()</code>返回一个<em>dictionary view</em>对象。从而支持如成员关系测试和迭代之类的操作,但是其内容并不是独立于原始字典的,只是一个<em>视图</em></p>
以上是[译]The Python Tutorial#Data Structures的详细内容。更多信息请关注PHP中文网其他相关文章!