地图的实际用例无填充功能? [英] Real-world use cases for map's None fill-in feature?

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

问题描述

提案

--------

我正在收集数据以评估替代版本的请求

itertools .izip()具有无填充功能,如内置的

map()函数:

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



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


动机是提供循环所有数据元素的方法
$ b当输入长度不相等时,$ b。当天的问题是

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

答案很可能在其他编程的结果中找到

语言和调查现实世界的Python代码。


其他语言

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

我扫描了Haskell,SML和Perl6的日元运算符的文档,发现

是map()和zip()的标准是截断到最短的输入

或者为不相等的输入长度引发异常。 Ruby采用

相反的方法并填写零值 -

设计选择背后的推理有点难以理解:
http://blade.nagaokaut.ac.jp/cgi -bin ... ruby​​-dev / 18651


真实世界代码

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

我扫描了标准库,我自己的代码和一些第三方

工具。我没有找到地图'的填充功能的实例。


zip的历史()

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

PEP 201(锁定步骤迭代)证明填写的功能是预计和拒绝的
对于Py2.0中引入的zip()内置。

在之前和之后的几年里,SourceForge日志没有显示

填充功能的请求。 br />

索取更多信息

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

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

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

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

以及被拒绝的替代方案(即填写功能是否为
改进代码)。同样,我很好奇是否有人看到

a拉链式填充功能在其他一些

编程语言中有效。


与SQL并行?

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

如果是迭代元素'' s序号位置被视为记录

键,然后该提议等同于数据库样式的全外连接

操作(其中一个包含结果中不匹配的键)记录

订单很重要。外连接是否与

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

理论愿望清单项目? Python需要itertools.izip_longest()

还是会成为一个令人分心的事情?


Raymond Hettinger

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())

解决方案

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

...

索取更多信息
--------------------- -------
我对comp.lang.python读者的请求是搜索你自己的代码
看看地图'的无填充功能是否曾在现实世界中使用过代码
(不是玩具示例)。我很好奇上下文,它是如何被使用的,
以及被拒绝的替代方案(即填充功能




我有(几年前,版本是1.5.2)一个真实的地图案例(最大,

seq1,seq2)。序列代表各种

特征的替代分数,使用没有意思是这个特征的得分不能通过用于产生这个序列的算法来计算,并且有一个序列更长的时候通常是

(使用一个后来开发的算法

,计算了更多的特征)。这个用法可能是滥用了我的

观察到max(None,N)和max(N,None)在我当时使用的

平台上总是N.我在Python上相对比较新,而且回想起来我觉得我可能会选择b $ b。使用所有新玩具

我们刚刚获得 - 循环特征索引来计算分数,

并明确地esting for None,可能是比建立这些列表更好的方法(使用seq1 = map(scorer1,range(N)),btw)然后

运行地图无论如何,在他们身上。无论如何,我后来迁移到一个懒惰的计算版本,不记得确切的细节,但它就像

一样(在今天的Python中) :


类LazyMergedList(对象):

def __init __(self,* fs):

self.fs = * fs

self.known = {}

def __getitem __(self,n):

尝试:return self.known [n]

除了KeyError:传递

result = self.known [n] = max(fs中f为f(n))

返回结果


当事实证明在大多数情况下下游代码实际上并没有使用所有功能(在每种情况下只是一小部分),所以

提前计算所有这些都是浪费周期。


我不记得曾经依赖地图'的无填充功能

现实世界的案例,而且,正如我所提到的,即使在这里,依赖也相当可疑。 OTOH,如果我能够轻松地指定一个不同的
填充物,我已经能够使用它几次了。

Alex


文章< ma ************************************ **@python.o rg>,

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

索取更多信息
----------------------------
我对comp.lang.python的读者的请求是搜索你自己的代码
看看地图'的无填充功能是否曾用于现实世界的代码
(不是玩具示例)。




我快速查看了我们的(Strakt')代码库并找到了一个例子。


代码是用于处理用户设计的宏,用户希望将数据附加到存储在系统中的字符串。请注意,所有数据都是存储为相关数据类型的列表。


虽然我没有写这段代码(所以我不能说什么,如果有的话,

的替代品被认为是),在我看来,它确实是最直接的-B
前进方式。能够说出填充值应该是什么,这将使代码更简单。


oldAttrVal是原始存储数据,而attValue是什么宏观

想要追加。


---> 8 ---

newAttrVal = []

for x,y in map(None,oldAttrVal,attrValue):

newAttrVal.append(u''''。join((x或'''',y或' ''')))

---> 8 ---


/ Anders


- -

- 我当然疯了,但这并不意味着我错了。

Anders Hammarquist | ik*@cd.chalmers.se

物理系学生,查尔姆斯理工大学,|哼:+46 31 88 48 50

G |瑞典teborg。 RADIO:SM6XMM和N2JGL | Mob:+46 707 27 86 87


[Alex Martelli]

我曾(多年前,版本是1.5.2)一个真实的世界地图案例(max,
seq1,seq2)。序列表示各种
特征的替代分数,使用无表示该特征的分数不能由用于产生该序列的算法计算,并且它是共同的。有一个序列更长(使用后来开发的算法计算更多的功能)。这种使用可能是滥用我的观察结果,我当时正在使用的
平台上max(None,N)和max(N,None)总是N.


分析

--------


该特定数据集有三个独特的方面,允许地图(最大,

s1,s2,s3)工作方法。


1)无可比拟的各种意义:

- 输入序列使用它来表示功能无法计算

- 无填充的自动填充意味着以后使用的功能
算法,但不是早期的那些

- 实现怪癖,其中max(None,n)== max(n,None)== n


2)使用像max()这样的缩减函数,它不关心输入的顺序(即输出序列并不表示哪个
算法产生了最佳分数)。


3)后来开发的序列必须使用

的知识创建所有早期序列使用的特征(以免其中两个序列
扩展了与之相对应的不同功能序数

位置。


绕过后一个限制建议使用映射

(feature->得分)而不是按顺序位置跟踪分数(

位置对应于特定功能):


bestscore = {}

for d in d1,d2,d3:

for feature,得分为d.iteritems():

bestscore [feature] = max(bestscore.get(feature,0),score这个方法也依赖于数据集中另外两个独特的
方面的方法。


使用dict.get()可以将任何对象指定为

一个默认值(对于
max()的空输入,零是更好的选择)。此外,模式不仅限于可交换减少

函数,如max();相反,它会与

result.setdefault(feature,[])。append(score)样式积累所有

结果或与其他组合/一起工作分析函数。


所以,虽然map'的无填充功能恰好适用于这个

数据集的独特功能,我想知道是否它的可用性引导你远离更好的数据结构,具有更大的灵活性,更少的b / b
依赖于怪癖,以及更多的通用性。


也许教训是外连接操作最好用词典而不是长度不等的序列来表示




我在Python中比较新,而且回想起来,我觉得我可能会选择使用我们刚刚获得的所有新玩具。


这表明如果itertools.zip_longest()对于许多任务来说并不是很好的话,那么它可能会生病 - 影响超出

只是愚蠢 - 它可能引导人们远离更好的解决方案。正如你知道的那样,Python新手可能需要一段时间来实现完整的功能和基于字典的方法的通用性。我想知道这个建议的迭代工具是否会分散这种意识。


我不记得曾经依赖地图'的无填充功能其他
现实世界的案例,正如我所提到的,即使在这里,依赖也相当可疑。 OTOH,如果我能够轻松指定不同的填充物,我可以使用它几次。




你跑了吗?通过建议的itertools.zip_longest()函数可以改进的任何食谱代码?

雷蒙德


Proposal
--------
I am gathering data to evaluate a request for an alternate version of
itertools.izip() with a None fill-in feature like that for 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 motivation 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 can likely be found in results from other programming
languages and from surveying real-world Python code.

Other languages
---------------
I scanned the docs for Haskell, SML, and Perl6''s yen operator and found
that the norm for map() and zip() is to truncate to the shortest input
or raise an exception for unequal input lengths. Ruby takes the
opposite approach and fills-in nil values -- the reasoning behind the
design choice is somewhat inscrutable:
http://blade.nagaokaut.ac.jp/cgi-bin...ruby-dev/18651

Real-world code
---------------
I scanned the standard library, my own code, and a few third-party
tools. I
found no instances where map''s fill-in feature was used.

History of zip()
----------------
PEP 201 (lock-step iteration) documents that a fill-in feature was
contemplated and rejected for the zip() built-in introduced in Py2.0.
In the years before and after, SourceForge logs show no requests for a
fill-in feature.

Request for more information
----------------------------
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). Likewise, I''m curious as to whether anyone has seen
a zip-style fill-in feature employed to good effect in some other
programming language.

Parallel to SQL?
----------------
If an iterator element''s ordinal position were considered as a record
key, then the proposal equates to a database-style full outer join
operation (one which includes unmatched keys in the result) where record
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? Does Python need itertools.izip_longest()
or would it just become a distracting piece of cruft?

Raymond Hettinger
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 straightforward but less 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> wrote:
...

Request for more information
----------------------------
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



I had (years ago, version was 1.5.2) one real-world case of map(max,
seq1, seq2). The sequences represented alternate scores for various
features, using None to mean "the score for this feature cannot be
computed by the algorithm used to produce this sequence", and it was
common to have one sequence longer (using a later-developed algorithm
that computed more features). This use may have been an abuse of my
observation that max(None, N) and max(N, None) were always N on the
platform I was using at the time. I was relatively new at Python, and
in retrospect I feel I might have been going for "use all the new toys
we''ve just gotten" -- looping on feature index to compute the scores,
and explicitly testing for None, might have been a better approach than
building those lists (with seq1=map(scorer1, range(N)), btw) and then
running map on them, anyway. At any rate, I later migrated to a lazily
computed version, don''t recall the exact details but it was something
like (in today''s Python):

class LazyMergedList(object):
def __init__(self, *fs):
self.fs = *fs
self.known= {}
def __getitem__(self, n):
try: return self.known[n]
except KeyError: pass
result = self.known[n] = max(f(n) for f in fs)
return result

when it turned out that in most cases the downstream code wasn''t
actually using all the features (just a small subset in each case), so
computing all of them ahead of time was a waste of cycles.

I don''t recall ever relying on map''s None-filling feature in other
real-world cases, and, as I mentioned, even here the reliance was rather
doubtful. OTOH, if I had easily been able to specify a different
filler, I _would_ have been able to use it a couple of times.
Alex


In article <ma**************************************@python.o rg>,
Raymond Hettinger <py****@rcn.com> wrote:

Request for more information
----------------------------
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 had a quick look through our (Strakt''s) codebase and found one example.

The code is used to process user-designed macros, where the user wants
to append data to strings stored in the system. Note that all data is
stored as lists of whatever the relevant data type is.

While I didn''t write this bit of code (so I can''t say what, if any,
alternatives were considered), it does seem to me the most straight-
forward way to do it. Being able to say what the fill-in value should
be would make the code even simpler.

oldAttrVal is the original stored data, and attValue is what the macro
wants to append.

--->8---
newAttrVal = []
for x, y in map(None, oldAttrVal, attrValue):
newAttrVal.append(u''''.join((x or '''', y or '''')))
--->8---

/Anders

--
-- Of course I''m crazy, but that doesn''t mean I''m wrong.
Anders Hammarquist | ik*@cd.chalmers.se
Physics student, Chalmers University of Technology, | Hem: +46 31 88 48 50
G|teborg, Sweden. RADIO: SM6XMM and N2JGL | Mob: +46 707 27 86 87


[Alex Martelli]

I had (years ago, version was 1.5.2) one real-world case of map(max,
seq1, seq2). The sequences represented alternate scores for various
features, using None to mean "the score for this feature cannot be
computed by the algorithm used to produce this sequence", and it was
common to have one sequence longer (using a later-developed algorithm
that computed more features). This use may have been an abuse of my
observation that max(None, N) and max(N, None) were always N on the
platform I was using at the time.
Analysis
--------

That particular dataset has three unique aspects allowing the map(max,
s1, s2, s3) approach to work at all.

1) Fortuitious alignment in various meanings of None:
- the input sequence using it to mean "feature cannot be computed"
- the auto-fillin of None meaning "feature used in later
algorithms, but not earlier ones"
- the implementation quirk where max(None, n) == max(n, None) == n

2) Use of a reduction function like max() which does not care about the
order of inputs (i.e. the output sequence does not indicate which
algorithm produced the best score).

3) Later-developed sequences had to be created with the knowledge of
the features used by all earlier sequences (lest two of the sequences
get extended with different features corresponding to the same ordinal
position).

Getting around the latter limitation suggests using a mapping
(feature->score) rather than tracking scores by ordinal position (with
position corresponding to a particular feature):

bestscore = {}
for d in d1, d2, d3:
for feature, score in d.iteritems():
bestscore[feature] = max(bestscore.get(feature, 0), score)

Such an approach also gets around dependence on the other two unique
aspects of the dataset. With dict.get() any object can be specified as
a default value (with zero being a better choice for a null input to
max()). Also, the pattern is not limited to commutative reduction
functions like max(); instead, it would work just as well with a
result.setdefault(feature, []).append(score) style accumulation of all
results or with other combining/analysis functions.

So, while map''s None fill-in feature happened to apply to this
dataset''s unique features, I wonder if its availability steered you
away from a better data-structure with greater flexibility, less
dependence on quirks, and more generality.

Perhaps the lesson is that outer-join operations are best expressed
with dictionaries rather than sequences with unequal lengths.

I was relatively new at Python, and
in retrospect I feel I might have been going for "use all the new toys
we''ve just gotten"
That suggests that if itertools.zip_longest() doesn''t turn out to be
TheRightTool(tm) for many tasks, then it may have ill-effects beyond
just being cruft -- it may steer folks away from better solutions. As
you know, it can take a while for Python newcomers to realize the full
power and generality of dictionary based approaches. I wonder if this
proposed itertool would distract from that realization.

I don''t recall ever relying on map''s None-filling feature in other
real-world cases, and, as I mentioned, even here the reliance was rather
doubtful. OTOH, if I had easily been able to specify a different
filler, I _would_ have been able to use it a couple of times.



Did you run across any cookbook code that would have been improved by
the proposed itertools.zip_longest() function?

Raymond


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

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