<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
On 12/25/2011 12:02 AM, daedae11 wrote:
<blockquote cite="mid:201112251302304454227@126.com" type="cite">
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<style>
BLOCKQUOTE {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; MARGIN-LEFT: 2em
}
OL {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
UL {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
P {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
BODY {
        LINE-HEIGHT: 1.5; FONT-FAMILY: &#24494;&#36719;&#38597;&#40657;; COLOR: #000000; FONT-SIZE: 10.5pt
}
</style>
<meta name="GENERATOR" content="MSHTML 8.00.7600.16891">
<div> </div>
<div>The build-in function reversed() in Python2.5 returns a
iterator. But I don't know how to use the iterator.</div>
<div>Please give me a simple example about how to use bulid-in
function reversed() to reverse a list.</div>
<br>
</blockquote>
>>> [x for x in reversed([1,2,3])]<br>
[3, 2, 1]<br>
>>> list(reversed([1,2,3]))<br>
[3, 2, 1]<br>
<br>
From the docs (it's all here though takes a bit of digging):<br>
<dl class="glossary docutils">
<dt id="term-iterator">iterator </dt>
<dd>
<p class="first">An object representing a stream of data.
Repeated calls to the iterator’s <a class="reference
external" title="next" href="library/functions.html#next"><tt
class="xref docutils literal"><span class="pre">next()</span></tt></a>
method return successive items in the stream. When no more
data are available a <a class="reference external"
title="exceptions.StopIteration"
href="library/exceptions.html#exceptions.StopIteration"><tt
class="xref docutils literal"><span class="pre">StopIteration</span></tt></a>
exception is raised instead. At this point, the iterator
object is exhausted and any further calls to its <a
class="reference external" title="next"
href="library/functions.html#next"><tt class="xref docutils
literal"><span class="pre">next()</span></tt></a> method
just raise <a class="reference external"
title="exceptions.StopIteration"
href="library/exceptions.html#exceptions.StopIteration"><tt
class="xref docutils literal"><span class="pre">StopIteration</span></tt></a>
again. Iterators are required to have an <a class="reference
external" title="object.__iter__"
href="reference/datamodel.html#object.__iter__"><tt
class="xref docutils literal"><span class="pre">__iter__()</span></tt></a>
method that returns the iterator object itself so every
iterator is also iterable and may be used in most places where
other iterables are accepted. One notable exception is code
which attempts multiple iteration passes. A container object
(such as a <a class="reference external" title="list"
href="library/functions.html#list"><tt class="xref docutils
literal"><span class="pre">list</span></tt></a>) produces
a fresh new iterator each time you pass it to the <a
class="reference external" title="iter"
href="library/functions.html#iter"><tt class="xref docutils
literal"><span class="pre">iter()</span></tt></a> function
or use it in a <a class="reference external"
href="reference/compound_stmts.html#for"><tt class="xref
docutils literal"><span class="pre">for</span></tt></a>
loop. Attempting this with an iterator will just return the
same exhausted iterator object used in the previous iteration
pass, making it appear like an empty container.</p>
<p class="last">More information can be found in <a
class="reference external"
href="library/stdtypes.html#typeiter"><em>Iterator Types</em></a>.</p>
</dd>
</dl>
<dl class="glossary docutils">
<dt id="term-iterable">iterable </dt>
<dd>A container object capable of returning its members one at a
time. Examples of iterables include all sequence types (such as
<a class="reference external" title="list"
href="library/functions.html#list"><tt class="xref docutils
literal"><span class="pre">list</span></tt></a>, <a
class="reference external" title="str"
href="library/functions.html#str"><tt class="xref docutils
literal"><span class="pre">str</span></tt></a>, and <a
class="reference external" title="tuple"
href="library/functions.html#tuple"><tt class="xref docutils
literal"><span class="pre">tuple</span></tt></a>) and some
non-sequence types like <a class="reference external"
title="dict" href="library/stdtypes.html#dict"><tt class="xref
docutils literal"><span class="pre">dict</span></tt></a> and
<a class="reference external" title="file"
href="library/functions.html#file"><tt class="xref docutils
literal"><span class="pre">file</span></tt></a> and objects
of any classes you define with an <a class="reference external"
title="object.__iter__"
href="reference/datamodel.html#object.__iter__"><tt
class="xref docutils literal"><span class="pre">__iter__()</span></tt></a>
or <a class="reference external" title="object.__getitem__"
href="reference/datamodel.html#object.__getitem__"><tt
class="xref docutils literal"><span class="pre">__getitem__()</span></tt></a>
method. Iterables can be used in a <a class="reference
external" href="reference/compound_stmts.html#for"><tt
class="xref docutils literal"><span class="pre">for</span></tt></a>
loop and in many other places where a sequence is needed (<a
class="reference external" title="zip"
href="library/functions.html#zip"><tt class="xref docutils
literal"><span class="pre">zip()</span></tt></a>, <a
class="reference external" title="map"
href="library/functions.html#map"><tt class="xref docutils
literal"><span class="pre">map()</span></tt></a>, ...). When
an iterable object is passed as an argument to the built-in
function <a class="reference external" title="iter"
href="library/functions.html#iter"><tt class="xref docutils
literal"><span class="pre">iter()</span></tt></a>, it
returns an iterator for the object. This iterator is good for
one pass over the set of values. When using iterables, it is
usually not necessary to call <a class="reference external"
title="iter" href="library/functions.html#iter"><tt
class="xref docutils literal"><span class="pre">iter()</span></tt></a>
or deal with iterator objects yourself. The <tt class="docutils
literal"><span class="pre">for</span></tt> statement does that
automatically for you, creating a temporary unnamed variable to
hold the iterator for the duration of the loop. See also <a
class="reference internal" href="#term-iterator"><em
class="xref">iterator</em></a>, <a class="reference
internal" href="#term-sequence"><em class="xref">sequence</em></a>,
and <a class="reference internal" href="#term-generator"><em
class="xref">generator</em></a>. </dd>
</dl>
<h2>7.3. The <a class="reference internal" href="#for"><tt
class="xref docutils literal"><span class="pre">for</span></tt></a>
statement</h2>
<p id="index-769">The <a class="reference internal" href="#for"><tt
class="xref docutils literal"><span class="pre">for</span></tt></a>
statement is used to iterate over the elements of a sequence (such
as a string, tuple or list) or other iterable object:</p>
<pre><strong id="grammar-token-for_stmt">for_stmt</strong> ::= "for" <a class="reference external" href="simple_stmts.html#grammar-token-target_list"><tt class="xref docutils literal"><span class="pre">target_list</span></tt></a> "in" <a class="reference external" href="expressions.html#grammar-token-expression_list"><tt class="xref docutils literal"><span class="pre">expression_list</span></tt></a> ":" <a class="reference internal" href="#grammar-token-suite"><tt class="xref docutils literal"><span class="pre">suite</span></tt></a>
["else" ":" <a class="reference internal" href="#grammar-token-suite"><tt class="xref docutils literal"><span class="pre">suite</span></tt></a>]
</pre>
<p>The expression list is evaluated once; it should yield an
iterable object. An iterator is created for the result of the <tt
class="docutils literal"><span class="pre">expression_list</span></tt>.
The suite is then executed once for each item provided by the
iterator, in the order of ascending indices. Each item in turn is
assigned to the target list using the standard rules for
assignments, and then the suite is executed. When the items are
exhausted (which is immediately when the sequence is empty), the
suite in the <a class="reference internal" href="#else"><tt
class="xref docutils literal"><span class="pre">else</span></tt></a>
clause, if present, is executed, and the loop terminates.</p>
<pre class="moz-signature" cols="72">--
Bob Gailer
919-636-4239
Chapel Hill NC</pre>
</body>
</html>