地图与列表理解 [英] map vs. list-comprehension

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

问题描述

你好,


受到最近一个线程的启发,其中Python中的reduce / map / lambda的结尾是讨论的,我查看了一些我的地图,并尝试将它们转换为

list-comprehensions。


这个我不知道如何融合:

给定三个长度为n,b,i和d的元组,我现在这样做:


map(lambda bb,ii,dd:bb + ii * dd,b ,我,d)


给出一个长度为n的列表。


任何人都知道等效列表理解是什么?


保重,

-

Mandus - 周围唯一的双节。

解决方案

Le Wed,2005年6月29日09:46:15 +0000(UTC),Mandusaécrit:

你好,

灵感来自最近的一个线程,其中讨论了Python中的reduce / map / lambda的结尾,我查看了一些我的地图,并尝试将它们转换为
list-comprehensions。
<这个我不知道如何融合:

给定三个长度为n,b,i和d的元组,我现在做:

map(lambda bb,ii,dd: bb + ii * dd,b,i,d)

给出一个长度为n的列表。




res = [bb + ii * dd代表bb,ii,dd代表拉链(b,i,d)]


希望不再弃用该拉链。


" F。珀蒂让" <李*********** @ news.free.fr>写道:

res = [bb + ii * dd为bb,ii,dd为zip(b,i,d)]

希望拉链将不得弃用。




没有人提出这个建议。计划删除的是

lambda,reduce,filter和map。这里是GvR的博客文章,解释了原因:

http://www.artima.com/weblogs/viewpost.jsp?thread=98196


-

Bj ?? rn Lindstr ?? m< bk ** @ stp.ling.uu.se>

乌普萨拉大学计算语言学学生,瑞典


2005年6月29日10:04:40 GMT skrev F. Petitjean:

Le Wed,2005年6月29日09:46:15 + 0000(UTC),Mandusaécrit:

嗨那里,

受到最近一个线程的启发,其中讨论了Python中reduce / map / lambda的结尾,我查看了一些地图,并尝试将它们转换为
list-comprehensions。

这个我不知道如何转换:

给定三个长度为n,b,i和d的元组,我现在这样做:

map(lambda bb,ii,dd:bb + ii * dd,b,i,d)

给出一个列表长度n。
res = [bb + ii * dd代表bb,ii,dd代表拉链(b,i,d)]




aha - 谢谢!我不知道拉链。我猜我必须把蟒蛇放在我的床头柜上(再次):-)


似乎比地图慢一点,但没什么严重。猜猜它是额外的拉链。

希望拉链不会被弃用。




希望不是......


-

Mandus - 周围唯一的双节。


Hi there,

inspired by a recent thread where the end of reduce/map/lambda in Python was
discussed, I looked over some of my maps, and tried to convert them to
list-comprehensions.

This one I am not sure how to conver:

Given three tuples of length n, b,i and d, I now do:

map(lambda bb,ii,dd: bb+ii*dd,b,i,d)

which gives a list of length n.

Anyone have an idea what the equivalent list comprehension will be?

take care,
--
Mandus - the only mandus around.

解决方案

Le Wed, 29 Jun 2005 09:46:15 +0000 (UTC), Mandus a écrit :

Hi there,

inspired by a recent thread where the end of reduce/map/lambda in Python was
discussed, I looked over some of my maps, and tried to convert them to
list-comprehensions.

This one I am not sure how to conver:

Given three tuples of length n, b,i and d, I now do:

map(lambda bb,ii,dd: bb+ii*dd,b,i,d)

which gives a list of length n.



res = [ bb+ii*dd for bb,ii,dd in zip(b,i,d) ]

Hoping that zip will not be deprecated.


"F. Petitjean" <li***********@news.free.fr> writes:

res = [ bb+ii*dd for bb,ii,dd in zip(b,i,d) ]

Hoping that zip will not be deprecated.



Nobody has suggested that. The ones that are planned to be removed are
lambda, reduce, filter and map. Here''s GvR''s blog posting that explains
the reasons:

http://www.artima.com/weblogs/viewpost.jsp?thread=98196

--
Bj??rn Lindstr??m <bk**@stp.ling.uu.se>
Student of computational linguistics, Uppsala University, Sweden


29 Jun 2005 10:04:40 GMT skrev F. Petitjean:

Le Wed, 29 Jun 2005 09:46:15 +0000 (UTC), Mandus a écrit :

Hi there,

inspired by a recent thread where the end of reduce/map/lambda in Python was
discussed, I looked over some of my maps, and tried to convert them to
list-comprehensions.

This one I am not sure how to conver:

Given three tuples of length n, b,i and d, I now do:

map(lambda bb,ii,dd: bb+ii*dd,b,i,d)

which gives a list of length n.
res = [ bb+ii*dd for bb,ii,dd in zip(b,i,d) ]



aha - thanks! I wasn''t aware of zip. Guess I have to put python in a
nutshell on my nightstand (again) :-)

seem to be a tad slower than the map, but nothing serious. Guess it''s
the extra zip.

Hoping that zip will not be deprecated.



hopefully not...

--
Mandus - the only mandus around.


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

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