[Tutor] question about the build-in function reversed in Python2.5
bob gailer
bgailer at gmail.com
Sun Dec 25 08:43:56 CET 2011
On 12/25/2011 12:02 AM, daedae11 wrote:
> The build-in function reversed() in Python2.5 returns a iterator. But
> I don't know how to use the iterator.
> Please give me a simple example about how to use bulid-in function
> reversed() to reverse a list.
>
>>> [x for x in reversed([1,2,3])]
[3, 2, 1]
>>> list(reversed([1,2,3]))
[3, 2, 1]
From the docs (it's all here though takes a bit of digging):
iterator
An object representing a stream of data. Repeated calls to the
iterator's next() <library/functions.html#next> method return
successive items in the stream. When no more data are available a
StopIteration <library/exceptions.html#exceptions.StopIteration>
exception is raised instead. At this point, the iterator object is
exhausted and any further calls to its next()
<library/functions.html#next> method just raise StopIteration
<library/exceptions.html#exceptions.StopIteration> again. Iterators
are required to have an __iter__()
<reference/datamodel.html#object.__iter__> 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 list <library/functions.html#list>)
produces a fresh new iterator each time you pass it to the iter()
<library/functions.html#iter> function or use it in a for
<reference/compound_stmts.html#for> 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.
More information can be found in /Iterator Types/
<library/stdtypes.html#typeiter>.
iterable
A container object capable of returning its members one at a time.
Examples of iterables include all sequence types (such as list
<library/functions.html#list>, str <library/functions.html#str>, and
tuple <library/functions.html#tuple>) and some non-sequence types
like dict <library/stdtypes.html#dict> and file
<library/functions.html#file> and objects of any classes you define
with an __iter__() <reference/datamodel.html#object.__iter__> or
__getitem__() <reference/datamodel.html#object.__getitem__> method.
Iterables can be used in a for <reference/compound_stmts.html#for>
loop and in many other places where a sequence is needed (zip()
<library/functions.html#zip>, map() <library/functions.html#map>,
...). When an iterable object is passed as an argument to the
built-in function iter() <library/functions.html#iter>, 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 iter() <library/functions.html#iter> or deal with iterator
objects yourself. The for statement does that automatically for you,
creating a temporary unnamed variable to hold the iterator for the
duration of the loop. See also /iterator/ <#term-iterator>,
/sequence/ <#term-sequence>, and /generator/ <#term-generator>.
7.3. The for <#for> statement
The for <#for> statement is used to iterate over the elements of a
sequence (such as a string, tuple or list) or other iterable object:
*for_stmt* ::= "for"target_list <simple_stmts.html#grammar-token-target_list> "in"expression_list <expressions.html#grammar-token-expression_list> ":"suite <#grammar-token-suite>
["else" ":"suite <#grammar-token-suite>]
The expression list is evaluated once; it should yield an iterable
object. An iterator is created for the result of the expression_list.
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 else <#else> clause, if present, is
executed, and the loop terminates.
--
Bob Gailer
919-636-4239
Chapel Hill NC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111225/24f3279d/attachment-0001.html>
More information about the Tutor
mailing list