可以列出理解替换地图吗? [英] can list comprehensions replace map?

查看:60
本文介绍了可以列出理解替换地图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手问题:


我一直对列表理解的建议持开放态

应该取代''map'',但我遇到了需要类似

map(无,x,y)
当len(x)> len(y)时
。我似乎不能使用''zip'',因为我会从x中丢失

信息。我如何将其作为列表理解? (或者,

一般来说,没有''地图''的最佳方法是什么?)


谢谢,

Alan Isaac

解决方案

David Isaac写道:

新手问题:

我有一般都对列表理解
应该取代''map''的提议持开放态度,但是当len(x)时我遇到了类似地图(无,x,y)
的需求)> LEN(Y)。我似乎不能使用''zip''因为我会丢失来自x的信息。我如何将其作为列表理解? (或者,更一般地说,没有''地图''的最佳方法是什么?)




它没有破坏所以我坚持你正在做的事情。即使map()是作为内置函数移除的
,它肯定会在模块中保留。

-

Michael Hoffman


Michael Hoffman写道:

David Isaac写道:

新手问题:
应该取代''map''的提议持开放态度,但我遇到了像
map(None,x,y)这样的需求
当len(x)> len(y)时。我似乎不能使用''zip''因为我会丢失来自x的信息。我如何将其作为列表理解? (或者,更一般地说,没有''地图''的最佳方法是什么?)



它没有破坏所以我会坚持你正在做什么。即使map()作为内置函数被删除,它肯定会在模块中保留。




附录:我知道这不会回答你的问题,所以如果你是纯粹的学术兴趣,那么其他人可能会发布另一个答案。

-

Michael Hoffman


这不是一个关于列表的问题

comprehensions,因为你正在使用功能

的地图通过传递无效作为

执行每个列表元素:


当len工作时(x)> len(y):


zip(x,y +(len(x)-len(y))* [无])


这当len(y)> = 0 len(x)时有效:


zip(x +(len(x)-len(y))* [无],y)


我可能会进入功能:


def foo(x,y):

if len(x )> len(y):

返回zip(x,y +(len(x)-len(y))* [无])


返回zip( x +(len(x)-len(y))* [无],y)


拉里贝茨


David Isaac写道:< blockquote class =post_quotes>新手问题:

我一直对列表理解
应该取代''map''的提议持开放态度,但我遇到了像
map(无,x,y)
当len(x)> len(y)时。我似乎不能使用''zip''因为我会丢失来自x的信息。我如何将其作为列表理解? (或者,更一般地说,没有''地图''的最佳方法是什么?)

谢谢,
Alan Isaac



Newbie question:

I have been generally open to the proposal that list comprehensions
should replace ''map'', but I ran into a need for something like
map(None,x,y)
when len(x)>len(y). I cannot it seems use ''zip'' because I''ll lose
info from x. How do I do this as a list comprehension? (Or,
more generally, what is the best way to do this without ''map''?)

Thanks,
Alan Isaac

解决方案

David Isaac wrote:

Newbie question:

I have been generally open to the proposal that list comprehensions
should replace ''map'', but I ran into a need for something like
map(None,x,y)
when len(x)>len(y). I cannot it seems use ''zip'' because I''ll lose
info from x. How do I do this as a list comprehension? (Or,
more generally, what is the best way to do this without ''map''?)



It ain''t broke so I''d stick with what you''re doing. Even if map() is
removed as a builtin, it will surely stick around in a module.
--
Michael Hoffman


Michael Hoffman wrote:

David Isaac wrote:

Newbie question:

I have been generally open to the proposal that list comprehensions
should replace ''map'', but I ran into a need for something like
map(None,x,y)
when len(x)>len(y). I cannot it seems use ''zip'' because I''ll lose
info from x. How do I do this as a list comprehension? (Or,
more generally, what is the best way to do this without ''map''?)


It ain''t broke so I''d stick with what you''re doing. Even if map() is
removed as a builtin, it will surely stick around in a module.



Addendum: I know this doesn''t answer your question, so if you were
asking out of purely academic interest, then someone else will probably
post another answer.
--
Michael Hoffman


This isn''t really a question about list
comprehensions as you are using a "feature"
of map by passing None as the function to be
executed over each list element:

This works when len(x) > len(y):

zip(x,y+(len(x)-len(y))*[None])

This works when len(y) >=0 len(x):

zip(x+(len(x)-len(y))*[None],y)

I would probably wrap into function:

def foo(x,y):
if len(x) > len(y):
return zip(x,y+(len(x)-len(y))*[None])

return zip(x+(len(x)-len(y))*[None],y)

Larry Bates

David Isaac wrote:

Newbie question:

I have been generally open to the proposal that list comprehensions
should replace ''map'', but I ran into a need for something like
map(None,x,y)
when len(x)>len(y). I cannot it seems use ''zip'' because I''ll lose
info from x. How do I do this as a list comprehension? (Or,
more generally, what is the best way to do this without ''map''?)

Thanks,
Alan Isaac



这篇关于可以列出理解替换地图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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