📄 node7.html
字号:
<H2><A NAME="SECTION007120000000000000000"></A><A NAME="lists-as-queues"></A><BR>5.1.2 把链表当作队列使用 Using Lists as Queues </H2><P>You can also use a list conveniently as a queue, where the first
element added is the first element retrieved (``first-in,
first-out''). To add an item to the back of the queue, use
<tt class="method">append()</tt>. To retrieve an item from the front of the queue,
use <tt class="method">pop()</tt> with <code>0</code> as the index. For example:<P>你也可以把链表当做队列使用,队列作为特定的数据结构,最先进入的元素最先释放(先进先出)。使用
<tt class="method">append()</tt>方法可以把元素添加到队列最后,以0为参数调用
<tt class="method">pop()</tt> 方法可以把最先进入的元素释放出来。例如:<P><div class="verbatim"><pre>
>>> queue = ["Eric", "John", "Michael"]
>>> queue.append("Terry") # Terry arrives
>>> queue.append("Graham") # Graham arrives
>>> queue.pop(0)
'Eric'
>>> queue.pop(0)
'John'
>>> queue
['Michael', 'Terry', 'Graham']</pre></div><P><H2><A NAME="SECTION007130000000000000000"></A><A NAME="functional"></A><BR>5.1.3 函数化编程工具 Functional Programming Tools </H2><P>There are three built-in functions that are very useful when used with
lists: <tt class="function">filter()</tt>, <tt class="function">map()</tt>, and <tt class="function">reduce()</tt>.<P>对于链表来讲,有三个内置函数非常有用:<tt class="function">filter()</tt>,
<tt class="function">map()</tt>, 和 <tt class="function">reduce()</tt>。<P>"<tt class="samp">filter(<var>function</var>, <var>sequence</var>)</tt>" returns a sequence (of
the same type, if possible) consisting of those items from the
sequence for which <code><var>function</var>(<var>item</var>)</code> is true. For
example, to compute some primes:<P>"<tt class="samp">filter(function, sequence)</tt>"返回一个序列(<var>sequence</var>),包括了给定序列中所有调用<code><var>function</var>(<var>item</var>)</code>后返回值为true的元素。(如果可能的话,会返回相同的类型)。例如,以下程序可以计算部分素数:<P><div class="verbatim"><pre>
>>> def f(x): return x % 2 != 0 and x % 3 != 0
...
>>> filter(f, range(2, 25))
[5, 7, 11, 13, 17, 19, 23]</pre></div><P>"<tt class="samp">map(<var>function</var>, <var>sequence</var>)</tt>" calls
<code><var>function</var>(<var>item</var>)</code> for each of the sequence's items and
returns a list of the return values. For example, to compute some
cubes:<P>"<tt class="samp">map(<var>function</var>, <var>sequence</var>)</tt>" 为每一个元素依次调用<code><var>function</var>(<var>item</var>)</code>并将返回值组成一个链表返回。例如,以下程序计算立方:<P><div class="verbatim"><pre>
>>> def cube(x): return x*x*x
...
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]</pre></div><P>More than one sequence may be passed; the function must then have as
many arguments as there are sequences and is called with the
corresponding item from each sequence (or <code>None</code> if some sequence
is shorter than another). For example:<P>可以传入多个序列,函数也必须要有对应数量的参数,执行时会依次用各序列上对应的元素来调用函数(如果某些序列比其它的短,就用<code>None</code>来代替)。如果把None做为一个函数传入,则直接返回参数做为替代。例如:<P><div class="verbatim"><pre>
>>> seq = range(8)
>>> def add(x, y): return x+y
...
>>> map(add, seq, seq)
[0, 2, 4, 6, 8, 10, 12, 14]</pre></div><P>"<tt class="samp">reduce(<var>func</var>, <var>sequence</var>)</tt>" returns a single value
constructed by calling the binary function <var>func</var> on the first two
items of the sequence, then on the result and the next item, and so
on. For example, to compute the sum of the numbers 1 through 10:<P>"<tt class="samp">reduce(<var>func</var>, <var>sequence</var>)</tt>"
返回一个单值,它是这样构造的:首先以序列的前两个元素调用函数,再以返回值和第三个参数调用,依次执行下去。例如,以下程序计算1到10的整数之和:<P><div class="verbatim"><pre>
>>> def add(x,y): return x+y
...
>>> reduce(add, range(1, 11))
55</pre></div><P>If there's only one item in the sequence, its value is returned; if
the sequence is empty, an exception is raised.<P>如果序列中只有一个元素,就返回它,如果序列是空的,就抛出一个异常。<P>A third argument can be passed to indicate the starting value. In this
case the starting value is returned for an empty sequence, and the
function is first applied to the starting value and the first sequence
item, then to the result and the next item, and so on. For example,<P>可以传入第三个参数做为初始值。如果序列是空的,就返回初始值,否则函数会先接收初始值和序列的第一个元素,然后是返回值和下一个元素,依此类推。例如:<P><div class="verbatim"><pre>
>>> def sum(seq):
... def add(x,y): return x+y
... return reduce(add, seq, 0)
...
>>> sum(range(1, 11))
55
>>> sum([])
0</pre></div><P>Don't use this example's definition of <tt class="function">sum()</tt>: since summing
numbers is such a common need, a built-in function
<code>sum(<var>sequence</var>)</code> is already provided, and works exactly like
this.
<span class="versionnote">New in version 2.3.</span><P>不要像示例中这样定义<tt class="function">sum()</tt>:因为合计数值是一个通用的需求,在2.3版中,提供了内置的<code>sum(<var>sequence</var>)</code> 函数。
<span class="versionnote">New in version 2.3.</span><P><H2><A NAME="SECTION007140000000000000000">5.1.4 链表推导式 List Comprehensions</A></H2><P>List comprehensions provide a concise way to create lists without resorting
to use of <tt class="function">map()</tt>, <tt class="function">filter()</tt> and/or <tt class="keyword">lambda</tt>.
The resulting list definition tends often to be clearer than lists built
using those constructs. Each list comprehension consists of an expression
followed by a <tt class="keyword">for</tt> clause, then zero or more <tt class="keyword">for</tt> or
<tt class="keyword">if</tt> clauses. The result will be a list resulting from evaluating
the expression in the context of the <tt class="keyword">for</tt> and <tt class="keyword">if</tt> clauses
which follow it. If the expression would evaluate to a tuple, it must be
parenthesized.<P>链表推导式提供了一个创建链表的简单途径,无需使用<tt class="function">map()</tt>, <tt class="function">filter()</tt> 以及 <tt class="keyword">lambda</tt>。返回链表的定义通常要比创建这些链表更清晰。每一个链表推导式包括在一个<tt class="keyword">for</tt> 语句之后的表达式,零或多个 <tt class="keyword">for</tt>或 <tt class="keyword">if</tt> 语句。返回值是由 <tt class="keyword">for</tt> 或 <tt class="keyword">if</tt>子句之后的表达式得到的元素组成的链表。如果想要得到一个元组,必须要加上括号。<P><div class="verbatim"><pre>
>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'loganberry', 'passion fruit']
>>> vec = [2, 4, 6]
>>> [3*x for x in vec]
[6, 12, 18]
>>> [3*x for x in vec if x > 3]
[12, 18]
>>> [3*x for x in vec if x < 2]
[]
>>> [[x,x**2] for x in vec]
[[2, 4], [4, 16], [6, 36]]
>>> [x, x**2 for x in vec] # error - parens required for tuples
File "<stdin>", line 1, in ?
[x, x**2 for x in vec]
^
SyntaxError: invalid syntax
>>> [(x, x**2) for x in vec]
[(2, 4), (4, 16), (6, 36)]
>>> vec1 = [2, 4, 6]
>>> vec2 = [4, 3, -9]
>>> [x*y for x in vec1 for y in vec2]
[8, 6, -18, 16, 12, -36, 24, 18, -54]
>>> [x+y for x in vec1 for y in vec2]
[6, 5, -7, 8, 7, -5, 10, 9, -3]
>>> [vec1[i]*vec2[i] for i in range(len(vec1))]
[8, 12, -54]</pre></div><P>List comprehensions are much more flexible than <tt class="function">map()</tt> and can be
applied to functions with more than one argument and to nested functions:<P>链表推导式比 <tt class="function">map()</tt>更复杂,可调用多个参数和嵌套函数。<P><div class="verbatim"><pre>
>>> [str(round(355/113.0, i)) for i in range(1,6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']</pre></div><P><H1><A NAME="SECTION007200000000000000000"></A><A NAME="del"></A><BR>5.2 <tt class="keyword">del</tt> 语句 </H1><P>There is a way to remove an item from a list given its index instead
of its value: the <tt class="keyword">del</tt> statement. This can also be used to
remove slices from a list (which we did earlier by assignment of an
empty list to the slice). For example:<P>有一个方法可从链表中删除指定索引的元素:<tt class="keyword">del</tt> 语句。这个方法也可以从链表中删除切片(之前我们是把一个空链表赋给切片)。例如:<P><div class="verbatim"><pre>
>>> a = [-1, 1, 66.6, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.6, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.6, 1234.5]</pre></div><P><tt class="keyword">del</tt> can also be used to delete entire variables:<P><tt class="keyword">del</tt> 也可以用于删除整个变量:<P><div class="verbatim"><pre>
>>> del a</pre></div><P>Referencing the name <code>a</code> hereafter is an error (at least until
another value is assigned to it). We'll find other uses for
<tt class="keyword">del</tt> later.<P>此后再引用这个名字会发生错误(至少要到给它赋另一个值为止)。后面我们还会发现<tt class="keyword">del</tt>的其它用法。<P><H1><A NAME="SECTION007300000000000000000"></A><A NAME="tuples"></A><BR>5.3 元组(Tuples)和序列(Sequences )Tuples and Sequences </H1><P>We saw that lists and strings have many common properties, such as
indexing and slicing operations. They are two examples of
<a class="ulink" href="../lib/typesseq.html" ><em>sequence</em> data types</a>. Since
Python is an evolving language, other sequence data types may be
added. There is also another standard sequence data type: the
<em>tuple</em>.<P>我们知道链表和字符串有很多通用的属性,例如索引和切片操作。它们是序列类型中的两种。因为Python是一个在不停进化的语言,也可能会加入其它的<a class="ulink" href="
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -