你有地图的无填充功能的真实用例吗? [英] Do you have real-world use cases for map's None fill-in feature?

查看:50
本文介绍了你有地图的无填充功能的真实用例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在评估一个替代版本的itertools.izip()

的请求,它具有像内置地图功能一样的无填充功能:

map(无,''abc'',''12345'')#demonst map'的无填充功能



[(''a'',''1''),(''b'',''2''),(''c '',''3'',(无,''4''),(无,''5'')]


移动是为了提供一种方法当输入长度不相等时,循环遍历所有数据元素

。当天的问题是

是否是一个普遍的需求和一个解决实际问题的好方法。

这个问题的答案很可能在结果中找到来自其他

编程语言或使用现实世界的Python代码

map''无填充功能。


我扫描了Haskell,SML和Perl的文档,发现map()和zip()的规范

是截断到最短的输入或者提高了
$ b不等输入长度的$ b异常。我扫描了标准库

,发现没有使用map'的填充功能的实例。同样地,

我在所有编写的代码中都没有找到示例。


Python当前zip()函数的历史作为另一个

指标,该提案很弱。 PEP 201考虑并拒绝了这个可能会产生意想不到的后果的想法。这些年来,自从在Py2.0中引入了zip()以来,

,SourceForge没有为zip()的填充版本显示请求




我对comp.lang.python的读者的请求是搜索你自己的代码

来看看地图'的无填充功能是否曾被用于实际世界代码

(不是玩具示例)。我很好奇上下文,它是如何使用的,

以及被拒绝的替代方案(即填写功能是否为
改进代码)。 />

另外,我很好奇是否有人看到拉链填充功能

在其他一些编程语言中使用效果很好,也许是

LISP或其他什么?


也许一些真实的单词代码示例和来自其他

语言的经验将阐明是否锁定步骤

迭代具有超出最短匹配长度的含义

元素。如果序号位置被视为记录键,则

提案等同于数据库样式的外连接操作(其中包含具有不匹配键的
数据元素)和顺序

重要。外连接是否与锁定步骤

迭代有关?这是一个基本的循环结构还是只是一个

理论愿望清单项目? IOW,Python真的需要

itertools.izip_longest()或者只会成为一个令人分心的部分

of cruft?

Raymond Hettinger

PS FWIW,OP的用例涉及在多个

栏中打印文件:


for f,g in itertools.izip_longest(file1,file2,fillin_value =''''):

打印''%-20s \ t | \t%-20s''%(f.rstrip(),g.rstrip())


替代方案是直截了当但不简洁:


而1:

f = file1.readline()

g = file2.readline()

如果不是f而不是g:

break

print''% - 20s \t | \t%-20s''%(f.rstrip(),g.rstrip())

解决方案

< blockquote>Raymond Hettinger < PY **** @ rcn.com>写道:

我正在评估一个替代版本的itertools.izip()
的请求,它具有像内置地图功能一样的无填充功能:
< blockquote class =post_quotes>

map(无,''abc'',''12345'')#demonst map'的无填充功能




我认为找到不同的写作方式是一个有趣的

练习,但它的用处有限,无法成为标准

功能。


我认为应该开发一些习惯用来检查

迭代器是否为空,而不消耗项目。这是一个想法:

介绍类似


iterator = check_empty(iterator)


其中check_empty大致会像(未经测试)一样工作:


def check_empty(iterator):

iclass = iterator .__ class__

class buffered( iclass):

def __init __(自我):

n = iter((self.next(),))#可能会提高StopIteration

self .__ save = chain(n,self)

def next(self):

返回self .__ save.next()

#所有其他操作继承自iclass


返回缓冲(迭代器)


想法是你得到一个新的迭代器,它产生相同的流

并且支持与旧的相同的操作,如果旧的是非b
非空。否则它会引发StopIteration。


上面有一些明显的问题:


1)新的迭代器应该支持所有旧的迭代器的属性,

不仅继承其操作

2)在旧迭代器已经缓冲的情况下,

构造函数应该只是偷看前瞻,而不是制作

a新对象。这意味着多次检查迭代器

不会燃烧越来越多的内存。


也许有一些方法可以使用元类进行上述操作但是我已经无法绕过那些了。


2006年1月7日23:19:41 -0800," ; Raymond Hettinger < PY **** @ rcn.com>写道:

我正在评估一个替代版本的itertools.izip()
的请求,它具有像内置地图功能一样的无填充功能: br>

map(无,''abc'',''12345'')#demonst map'的无填充功能[(''' a'',''1'',(''b'',''2''),(''c'',''3''),(无,''4''),(没有,''5'')

我不喜欢无法提供自己的哨兵。没有太常见

一个值。嗯,......< bf警告>除非也许它也可以是我们可以实例化的类型

真正意味着它的上下文级别如None(5);-)

map(无(5),''abc'',''12345'')#演示地图'的无填充功能



[( ''a'',''1'',(''b'',''2''),(''c'',''3''),(无(5),''4 ''),(无(5),''5'')]


但严重的是,缺少数据的标准哨兵和数据结束可能很高兴,

并且在适当的标准环境中生成。单例字符串子类

实例NOD和EOD?但是不适合EOF ==''''。

< / bf警告>

移动是为循环所有数据元素提供一种方法
当输入长度不相等时。当天的问题是,这是否是一个共同的需求和对现实世界问题的良好方法。
问题的答案可能在其他编程语言的结果中找到或者来自使用
map'的无填充功能的真实Python代码。

如我的izip2中的某些语义怎么样? http://groups.google.com/group/comp。 ... 1ddb1f46?hl = en


(它甚至不需要单独的名称,因为它可以向后兼容)

此外,将序列相关的东西分解为方法或属性

的iter实例怎么样?并让它采取多个序列或可调用/哨兵对,

可以替代izip然后一些?可以在第一次.next()调用之前或之后通过返回的

迭代器调用方法来控制各种功能,例如

哨兵测试''是''而不是'==''代表可调用/哨兵对,或缓冲n b / b $。前调步骤支持的.peek(n)方法默认为.peek(1)等等。 br />
关键是要有一个实现通用序列的地方。

我扫描了Haskell,SML和Perl的文档,发现了地图的标准
()和zip()是截断到最短输入或引发不等输入长度的异常。我扫描了标准库
,发现没有使用map'填充功能的实例。同样地,我没有在我写过的所有代码中找到任何例子。

Python当前的zip()函数的历史作为另一个
指标该提案很薄弱。 PEP 201考虑并拒绝将这一想法视为可能产生意想不到的后果的想法。由于zip()是在Py2.0中引入的,因此SourceForge对zip()的填充版本没有请求


我对读者的要求comp.lang.python是搜索你自己的代码
,看看地图'的无填充功能是否曾用于现实世界的代码
(不是玩具示例)。我很好奇上下文,它是如何被使用的,
以及被拒绝的替代方案(即填写功能是否改进了代码)。

此外,我很好奇是否有人看到拉链填充功能
在其他一些编程语言中使用效果很好,也许是LISP或者其他什么?
ISTM一般存在鸡蛋问题,解决方法很容易。

即,真正的问题是有多少可用的解决方法

使用内置功能方便地处理,

和_then_看看便利性是否足够值。
也许一些真实的单词代码示例和其他
语言的经验将会揭示锁定步骤是否具有超出最短匹配元素长度的意义的问题。如果序号位置被视为记录键,那么该提议等同于数据库样式的外连接操作(其中包含具有不匹配键的数据元素)并且顺序非常重要。外连接是否与锁步重复有关?这是一个基本的循环结构还是只是理论上的愿望清单项? IOW,Python真的需要吗?/或者它会成为一个令人分心的东西吗?



即使很少用于继续在正确的代码中,IWT在错误的情况下在迭代器的状态下得到
将是一个好处。

能够看到最后一次尝试收集元组的结果元素

可以提供帮助。 (我可以看到想要尝试所有流的变化的理由

与第一次排出的快捷方式虽然耗尽)。


问候,

Bengt Richter


[Bengt Richter]

像我的izip2一样的语义如何 http://groups.google.com/group/comp .. ..1ddb1f46?hl = en

(它甚至不需要单独的名称,因为它可以向后兼容)

另外,怎么样将序列相关的东西分解为iter实例的方法或属性?让它采取多个序列或可调用/哨兵对,
可以替代izip,然后一些?可以在第一次.next()调用之前或之后通过返回的
迭代器调用方法来控制各种功能,例如通过''is'而不是''==''进行哨兵测试对于可调用/哨兵对,或缓冲前瞻性步骤,由.peek(n)方法支持默认为.peek(1)等等。
关键是要有一个实施的地方通用序列的东西。


ISTM,这些疗法比疾病更糟糕;-)


即使没有多少用于继续正确的代码,IWT获得
处于错误状态的迭代器状态将是一个好处。
能够看到最后一次尝试收集元组元素的结果
可能会有所帮助。 (我可以看到想要尝试所有流的变化的原因
与第一个的快捷方式虽然耗尽)。




一方面,这似乎是合理的。另一方面,我看不到

如何使用它而不会咆哮周围的代码,在这种情况下,它可能更好地明确管理单个迭代器

while while。

Raymond


I am evaluating a request for an alternate version of itertools.izip()
that has a None fill-in feature like the built-in map function:

map(None, ''abc'', ''12345'') # demonstrate map''s None fill-in feature


[(''a'', ''1''), (''b'', ''2''), (''c'', ''3''), (None, ''4''), (None, ''5'')]

The movitation is to provide a means for looping over all data elements
when the input lengths are unequal. The question of the day is whether
that is both a common need and a good approach to real-world problems.
The answer to the question can likely be found in results from other
programming languages or from real-world Python code that has used
map''s None fill-in feature.

I scanned the docs for Haskell, SML, and Perl and found that the norm
for map() and zip() is to truncate to the shortest input or raise an
exception for unequal input lengths. I scanned the standard library
and found no instances where map''s fill-in feature was used. Likewise,
I found no examples in all of the code I''ve ever written.

The history of Python''s current zip() function serves as another
indicator that the proposal is weak. PEP 201 contemplated and rejected
the idea as one that likely had unintended consequences. In the years
since zip() was introduced in Py2.0, SourceForge has shown no requests
for a fill-in version of zip().

My request for readers of comp.lang.python is to search your own code
to see if map''s None fill-in feature was ever used in real-world code
(not toy examples). I''m curious about the context, how it was used,
and what alternatives were rejected (i.e. did the fill-in feature
improve the code).

Also, I''m curious as to whether someone has seen a zip fill-in feature
employed to good effect in some other programming language, perhaps
LISP or somesuch?

Maybe a few real-word code examples and experiences from other
languages will shed light on the question of whether lock-step
iteration has meaning beyond the length of the shortest matching
elements. If ordinal position were considered as a record key, then
the proposal equates to a database-style outer join operation (where
data elements with unmatched keys are included) and order is
significant. Does an outer-join have anything to do with lock-step
iteration? Is this a fundamental looping construct or just a
theoretical wish-list item? IOW, does Python really need
itertools.izip_longest() or would that just become a distracting piece
of cruft?
Raymond Hettinger
P.S. FWIW, the OP''s use case involved printing files in multiple
columns:

for f, g in itertools.izip_longest(file1, file2, fillin_value=''''):
print ''%-20s\t|\t%-20s'' % (f.rstrip(), g.rstrip())

The alternative was straight-forward but not as terse:

while 1:
f = file1.readline()
g = file2.readline()
if not f and not g:
break
print ''%-20s\t|\t%-20s'' % (f.rstrip(), g.rstrip())

解决方案

"Raymond Hettinger" <py****@rcn.com> writes:

I am evaluating a request for an alternate version of itertools.izip()
that has a None fill-in feature like the built-in map function:

map(None, ''abc'', ''12345'') # demonstrate map''s None fill-in feature



I think finding different ways to write it was an entertaining
exercise but it''s too limited in usefulness to become a standard
feature.

I do think some idiom ought to develop to allow checking whether an
iterator is empty, without consuming an item. Here''s an idea:
introduce something like

iterator = check_empty(iterator)

where check_empty would work roughly like (untested):

def check_empty(iterator):
iclass = iterator.__class__
class buffered(iclass):
def __init__(self):
n = iter((self.next(),)) # might raise StopIteration
self.__save = chain(n, self)
def next(self):
return self.__save.next()
# all other operations are inherited from iclass

return buffered(iterator)

The idea is you get back a new iterator which yields the same stream
and supports the same operations as the old one, if the old one is
non-empty. Otherwise it raises StopIteration.

There are some obvious problems with the above:

1) the new iterator should support all of the old one''s attributes,
not just inherit its operations
2) In the case where the old iterator is already buffered, the
constructor should just peek at the lookahead instead of making
a new object. That means that checking an iterator multiple times
won''t burn more and more memory.

Maybe there is some way of doing the above with metaclasses but I''ve
never been able to wrap my head around those.


On 7 Jan 2006 23:19:41 -0800, "Raymond Hettinger" <py****@rcn.com> wrote:

I am evaluating a request for an alternate version of itertools.izip()
that has a None fill-in feature like the built-in map function:

map(None, ''abc'', ''12345'') # demonstrate map''s None fill-in feature[(''a'', ''1''), (''b'', ''2''), (''c'', ''3''), (None, ''4''), (None, ''5'')]
I don''t like not being able to supply my own sentinel. None is too common
a value. Hm, ... <bf warning> unless maybe it can also be a type that we can instantiate with
really-mean-it context level like None(5) ;-)

map(None(5), ''abc'', ''12345'') # demonstrate map''s None fill-in feature


[(''a'', ''1''), (''b'', ''2''), (''c'', ''3''), (None(5), ''4''), (None(5), ''5'')]

But seriously, standard sentinels for "missing data" and "end of data" might be nice to have,
and to have produced in appropriate standard contexts. Singleton string subclass
instances "NOD" and "EOD"? Doesn''t fit with EOF=='''' though.
</bf warning>
The movitation is to provide a means for looping over all data elements
when the input lengths are unequal. The question of the day is whether
that is both a common need and a good approach to real-world problems.
The answer to the question can likely be found in results from other
programming languages or from real-world Python code that has used
map''s None fill-in feature.
What about some semantics like my izip2 in
http://groups.google.com/group/comp....1ddb1f46?hl=en

(which doesn''t even need a separate name, since it would be backwards compatible)

Also, what about factoring sequence-related stuff into being methods or attributes
of iter instances? And letting iter take multiple sequences or callable/sentinel pairs,
which could be a substitute for izip and then some? Methods could be called via a returned
iterator before or after the first .next() call, to control various features, such as
sentinel testing by ''is'' instead of ''=='' for callable/sentinel pairs, or buffering n
steps of lookahead supported by a .peek(n) method defaulting to .peek(1), etc. etc.
The point being to have a place to implement universal sequence stuff.
I scanned the docs for Haskell, SML, and Perl and found that the norm
for map() and zip() is to truncate to the shortest input or raise an
exception for unequal input lengths. I scanned the standard library
and found no instances where map''s fill-in feature was used. Likewise,
I found no examples in all of the code I''ve ever written.

The history of Python''s current zip() function serves as another
indicator that the proposal is weak. PEP 201 contemplated and rejected
the idea as one that likely had unintended consequences. In the years
since zip() was introduced in Py2.0, SourceForge has shown no requests
for a fill-in version of zip().

My request for readers of comp.lang.python is to search your own code
to see if map''s None fill-in feature was ever used in real-world code
(not toy examples). I''m curious about the context, how it was used,
and what alternatives were rejected (i.e. did the fill-in feature
improve the code).

Also, I''m curious as to whether someone has seen a zip fill-in feature
employed to good effect in some other programming language, perhaps
LISP or somesuch? ISTM in general there is a chicken-egg problem where workarounds are easy.
I.e., the real question is how many workaround situations there are
that would have been handled conveniently with a builtin feature,
and _then_ to see whether the convenience would be worth enough.
Maybe a few real-word code examples and experiences from other
languages will shed light on the question of whether lock-step
iteration has meaning beyond the length of the shortest matching
elements. If ordinal position were considered as a record key, then
the proposal equates to a database-style outer join operation (where
data elements with unmatched keys are included) and order is
significant. Does an outer-join have anything to do with lock-step
iteration? Is this a fundamental looping construct or just a
theoretical wish-list item? IOW, does Python really need
itertools.izip_longest() or would that just become a distracting piece
of cruft?


Even if there is little use for continuing in correct code, IWT getting
at the state of the iterator in an erroroneous situation would be a benefit.
Being able to see the result of the last attempt at gathering tuple elements
could help. (I can see reasons for wanting variations of trying all streams
vs shortcutting on the first to exhaust though).

Regards,
Bengt Richter


[Bengt Richter]

What about some semantics like my izip2 in
http://groups.google.com/group/comp....1ddb1f46?hl=en

(which doesn''t even need a separate name, since it would be backwards compatible)

Also, what about factoring sequence-related stuff into being methods or attributes
of iter instances? And letting iter take multiple sequences or callable/sentinel pairs,
which could be a substitute for izip and then some? Methods could be called via a returned
iterator before or after the first .next() call, to control various features, such as
sentinel testing by ''is'' instead of ''=='' for callable/sentinel pairs, or buffering n
steps of lookahead supported by a .peek(n) method defaulting to .peek(1), etc. etc.
The point being to have a place to implement universal sequence stuff.
ISTM, these cures are worse than the disease ;-)

Even if there is little use for continuing in correct code, IWT getting
at the state of the iterator in an erroroneous situation would be a benefit.
Being able to see the result of the last attempt at gathering tuple elements
could help. (I can see reasons for wanting variations of trying all streams
vs shortcutting on the first to exhaust though).



On the one hand, that seems reasonable. On the other hand, I can''t see
how to use it without snarling the surrounding code in which case it is
probably better to explicitly manage individual iterators within a
while loop.
Raymond


这篇关于你有地图的无填充功能的真实用例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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