映射无。为什么? [英] Mapping None. Why?

查看:61
本文介绍了映射无。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道为什么地图的特殊行为当

中的函数给出为无:


帮助构建-in函数图__builtin __:


map(...)

map(函数,序列[,序列,...])-list


返回将函数应用于项目的结果列表


参数序列。如果给出了多个序列,则使用参数列表调用

函数,该列表由每个序列的

对应的

项替换

不是全部

序列具有相同的长度时没有缺失值。如果函数为None,则返回

列表中的序列项目(或元组列表,如果多于一个

序列) )。

似乎没有任何动作与使用

lambda * x:x

的函数相同如下所示例如:


>> l1 =''asdf''
l2 =''qwertyuip''
l3 =范围(3)
l1,l2,l3



( ''asdf'',''qwertyuip'',[0,1,2])


>> map(lambda * x:x,l1,l2,l3)== map(无,l1,l2,l3)



True


>>>



在维基百科上查找地图时,没有提及这种特殊的

行为,
所以我的问题是为什么?


谢谢,Paddy。

解决方案

< blockquote> Paddy schrieb:


我想知道为什么地图的特殊行为当

中的函数给出为无:


模块中内置函数映射的帮助__builtin __:


map(...)

map(函数,序列[ ,序列,...])-list


返回将函数应用于项目的结果列表



参数序列。如果给出了多个序列,则使用参数列表调用

函数,该列表由每个序列的

对应的

项替换

不是全部

序列具有相同的长度时没有缺失值。如果函数为None,则返回

列表中的序列项目(或元组列表,如果多于一个

序列) )。


似乎没有任何动作与使用

lambda * x:x

的函数相同如下例所示:


>>> l1 =''asdf''
l2 =' 'qwertyuip''
l3 =范围(3)
l1,l2,l3



(''asdf'', ''qwertyuip'',[0,1,2])


>>> map(lambda * x: x,l1,l2,l3)== map(无,l1,l2,l3)



True


在维基百科上查找地图时,没有提到这种特殊的

行为,

所以我的问题是为什么?



因为未定义在没有给出函数的情况下应该发生什么

- 并且因为python中没有身份函数

预先定义,将该函数设为无效的
可能被认为是明智的。


它只能遵循* if *你暗示一个函数,即使有

没有给出,传递的元组被返回。


我在维基百科上看不到任何东西定义任何其他行为。


Diez


Diez


On Thu, 2008年6月12日下午1:05,Paddy< pa ******* @ googlemail.com写道:


>

我想知道为什么地图的特殊行为当

中的函数给出为无:



因为这就是它的方式一直都是!说真的,我不知道。我可以告诉你它在Python 3.0中已经消失了。


Ian


2008年6月12日星期四下午1:32,Diez B. Roggisch< de *** @ nospam.web.dewrote:


因为它是未定义的,如果在
all中没有给出任何函数,应该发生什么 - 并且因为预定义的python中没有身份函数,所以

可以被认为是合理的make None是该函数的等价物。



在传递非函数时引发错误更有意义

其中函数是预期的。如果我们要为None做一个特殊的

行为,为什么不能有True,False,

和42的特殊行为?缺少内置包装的正确解决方案

(非身份)功能是定义一个包装功能,而不是为了特殊情况下的
特殊情况下的任意值*表示*包装功能在某些情况下为



Ian



Iam wondering why the peculiar behavior of map when the function in
given as None:

Help on built-in function map in module __builtin__:

map(...)
map(function, sequence[, sequence, ...]) -list

Return a list of the results of applying the function to the items
of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the
corresponding
item of each sequence, substituting None for missing values when
not all
sequences have the same length. If the function is None, return a
list of
the items of the sequence (or a list of tuples if more than one
sequence).
It seems as the action whith none is the same as using a function of
lambda *x: x
As in the following example:

>>l1 = ''asdf''
l2 = ''qwertyuip''
l3 = range(3)
l1,l2,l3

(''asdf'', ''qwertyuip'', [0, 1, 2])

>>map(lambda *x: x, l1,l2,l3) == map(None, l1,l2,l3)

True

>>>


On looking up map on Wikipedia there is no mention of this special
behaviour,
So my question is why?

Thanks, Paddy.

解决方案

Paddy schrieb:

Iam wondering why the peculiar behavior of map when the function in
given as None:

Help on built-in function map in module __builtin__:

map(...)
map(function, sequence[, sequence, ...]) -list

Return a list of the results of applying the function to the items
of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the
corresponding
item of each sequence, substituting None for missing values when
not all
sequences have the same length. If the function is None, return a
list of
the items of the sequence (or a list of tuples if more than one
sequence).
It seems as the action whith none is the same as using a function of
lambda *x: x
As in the following example:

>>>l1 = ''asdf''
l2 = ''qwertyuip''
l3 = range(3)
l1,l2,l3

(''asdf'', ''qwertyuip'', [0, 1, 2])

>>>map(lambda *x: x, l1,l2,l3) == map(None, l1,l2,l3)

True
On looking up map on Wikipedia there is no mention of this special
behaviour,
So my question is why?

Because it is undefined what should happen in case of no function given
at all - and because there is no identity function in python
pre-defined, it could be considered sensible to make None the quivalent
of that function.

And it only follows that *if* you imply a function even though there is
None given, that the passed tuple is returned.

I don''t see anything on wikipedia that defines any other behavior.

Diez

Diez


On Thu, Jun 12, 2008 at 1:05 PM, Paddy <pa*******@googlemail.comwrote:

>
Iam wondering why the peculiar behavior of map when the function in
given as None:

Because that''s the way it''s always been! Seriously, I don''t know. I
can tell you that it''s going away in Python 3.0, though.

Ian


On Thu, Jun 12, 2008 at 1:32 PM, Diez B. Roggisch <de***@nospam.web.dewrote:

Because it is undefined what should happen in case of no function given at
all - and because there is no identity function in python pre-defined, it
could be considered sensible to make None the quivalent of that function.

It makes more sense to raise an error when a non-function is passed
where a function is expected. If we''re going to have a special
behaviour for None, why not have special behaviours for True, False,
and 42 as well? The proper solution to the lack of a built-in packing
(not identity) function is to define a packing function, not to
special-case an arbitrary value to *mean* the packing function in
certain situations.

Ian


这篇关于映射无。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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