English 中文(简体)
B. 部分辩论的适用图
原标题:Applying map for partial argument
  • 时间:2012-04-25 11:41:52
  •  标签:
  • python

Given the following function f with two arguments, what is the standard way to apply map to only x?

def f (x,y):
    print x,y

更具体地说,我要以一线地图开展以下行动。

list=[1,2,3]
fixed=10
for s in list:
    f(s,fixed)

这样做的一个途径是:

import functools
map(functools.partial(lambda x,y:f(y,x),fixed),list)

更好的方式是什么?

问题回答

首先,没有必要使用斜线和局部,它们是替代物:

map(lambda x:f(x,fixed),srclist)

第二,只要你知道以下论点,你就只能将第二点与<条码>部分<>/代码”联系起来:

map(functools.partial(f,y=fixed),srclist)

或者使用一个清单理解:

[f(x, fixed) for x in srclist]

鉴于以下职能有两个论点,对地图适用的标准方法是什么?

A 很少讨论治疗和部分应用<>。

In FP terms, your function f is "uncurried" - while it conceptually takes two arguments, they re bundled in a single product structure. In Python, everything is uncurried, all the time. You have to give all the arguments at once, or none of them.

为了围绕这一点开展工作,有各种陷阱,但从概念上来说,你只是想“保证”这一功能。 也就是说,将<代码>f(x,y)改为f(x),其中回收新的功能<代码>g(y)。

以因缺省而治愈的语文,您可以轻松地撰写这一译文:

-- curry: take a function from (x,y) to one from x to a function from y to z
curry :: ((x,y) -> z) -> (x -> y -> z)
curry f x y     = f (x, y)

So curry 请分别使用<<条码>f及其产品论点,并在所有理由均可获得时适用这些论点。 反之亦然:

uncurry :: (x -> y -> z) -> ((x,y) -> z)
uncurry f (x,y) =  f x y

这与部分适用有何关系?

  • Currying takes a function that takes a structure of 1 (n-product) argument, and returns a new function taking n arguments.
  • Partial application takes a function of n arguments and applies them to k arguments, yielding a function of n-k remaining arguments.

可以用一种未加收的语文提出每一论点(例如,部分涉及职能的性质)。 如上述例子所示,用粗略语言,你必须首先玩笑不动。

我认为,由于部分应用是免费的,因此,更灵活地进入一个 cur的环境下。 在这种环境中,通常将以下职能合并起来:>。 举例说,一管重整:

(+1) . (*2) . (^3) $ 7

只是部分适用、未经采购的职能的链条,每个功能都根据先前的职能的产出运作。 这可能是空洞的,因为它视而不见。

map(lambda x: f(fixed,x), thelist)

Or don t even use map() at all, use list comprehensions instead.

[f(fixed, x) for x in thelist]

PS:不使用<代码>list作为可变名称,它具有一个重要的内联网名称(清单类型)。

:

def add(x, y):
    return x + y

l = [1, 2, 3]

from functools import partial
plus10 = map(partial(add, y=10), l)
print plus10

## [11, 12, 13]




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签