Pre-PEP:反向迭代方法 [英] Pre-PEP: reverse iteration methods

查看:88
本文介绍了Pre-PEP:反向迭代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是潜在PEP的讨论草案。

这些想法源于对pep-284的讨论。

邀请评论。飞镖投掷是可选的。

Raymond Hettinger


----------------------- --------------------------------------


PEP:323

标题:添加反向迭代方法

版本:$修订版:1.1 $

最后修改:$ Date:2003/03 / 11 04:49:44 $

作者:Raymond Hettinger< python at rcn.com>

状态:草稿

类型:标准跟踪

内容类型:text / x-rst

创建时间:2003年9月23日

Python-Version:2.4

后历史:2003年9月23日

摘要

========


这个提议是扩展几个序列类型的API

以包括反向迭代序列的方法。

动机

=== =======


对于可索引对象,反向迭代的当前方法是

容易出错,不自然,不易读取::

$ x $ b for x in xrange(n-1,-1,-1):

print seqn [i]


另一种当前方法涉及在迭代之前反转列表

。这种技术浪费了计算机周期,内存和

代码。此外,它只适用于列表(字符串,例如,不定义

a反向方法)::


rseqn = list(seqn)

rseqn.reverse()
rseqn中元素的


打印元素


反向迭代少得多常见于前向迭代,但它确实在实践中经常出现


建议

========


添加一个名为iter_backwards()的方法来对可以从中受益的对象进行排序。上面的例子然后简化为::


for x in xrange(n).iter_backwards():

print seqn [i]
<对于seqn.iter_backwards()中的elem,


print elem


新协议将应用于列表,字符串,xranges对象,

以及可能的其他序列对象(取决于用例

和实现问题)。它不适用于无序集合

喜欢dicts和sets。


不需要更改语言语法。

未解决的问题< br $>
===========


*是否应该包含元组?在过去,他们被拒绝了一些列表

之类的行为,比如count()和index()。


*是否应该包含文件对象?实现反向迭代可能不会很容易,虽然它偶尔会有用。


*应该包括enumerate()吗?每当底层序列支持它时,它只会提供反向迭代



版权

=========

本文档已被置于公共领域。

解决方案

修订版:1.1



Last-Modified:


日期:2003/03/11 04:49:44

Here is a discussion draft of a potential PEP.
The ideas grew out of the discussion on pep-284.
Comments are invited. Dart throwing is optional.
Raymond Hettinger

-------------------------------------------------------------

PEP: 323
Title: Add Reverse Iteration Methods
Version: $Revision: 1.1 $
Last-Modified: $Date: 2003/03/11 04:49:44 $
Author: Raymond Hettinger <python at rcn.com>
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 23-Sep-2003
Python-Version: 2.4
Post-History: 23-Sep-2003
Abstract
========

This proposal is to extend the API of several sequence types
to include methods for iterating over the sequence in reverse.
Motivation
==========

For indexable objects, current methods for reverse iteration are
error prone, unnatural, and not especially readable::

for i in xrange(n-1, -1, -1):
print seqn[i]

One other current approach involves reversing a list before iterating
over it. That technique wastes computer cycles, memory, and lines of
code. Also, it only works with lists (strings, for example, do not define
a reverse method)::

rseqn = list(seqn)
rseqn.reverse()
for elem in rseqn:
print elem

Reverse iteration is much less common than forward iteration, but it
does arise regularly in practice.
Proposal
========

Add a method called iter_backwards() to sequence objects that can benefit
from it. The above examples then simplify to::

for i in xrange(n).iter_backwards():
print seqn[i]

for elem in seqn.iter_backwards():
print elem

The new protocol would be applied to lists, strings, xranges objects,
and possibly other sequence objects as well (depending on use cases
and implementation issues). It would not apply to unordered collections
like dicts and sets.

No language syntax changes are needed.
Open Issues
===========

* Should tuples be included? In the past they have been denied some list
like behaviors such as count() and index().

* Should file objects be included? Implementing reverse iteration may not
be easy though it would be useful on occasion.

* Should enumerate() be included? It would only provide reverse iteration
whenever the underlying sequence supported it.
Copyright
=========

This document has been placed in the public domain.

解决方案

Revision: 1.1



Last-Modified:


Date: 2003/03/11 04:49:44


这篇关于Pre-PEP:反向迭代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆