简体   繁体   English

Python 调用转换函数的最佳方式

[英]Python best way to call transformative functions

I'm creating a transformative function that takes two lists.我正在创建一个具有两个列表的变革性 function。

one list has the values that I want to transform and the second list has a list of functions that I want to send the values to in order.一个列表包含我想要转换的值,第二个列表包含我想要按顺序将值发送到的函数列表。

def directing_function(value_list, func_list):
    value = None
    transformed = []
    for x in value_list:
        value = func_list[0](x)
        for y in func_list[1:]:
            value = y(value)
        transformed.append(value)
    return transformed

Granted this is a mockup as I haven't written it exactly.当然这是一个模型,因为我没有完全写出来。 I'm trying to figure out the best way to reference the possible functions.我试图找出引用可能功能的最佳方法。 I considered Enum, a dictionary for case...我考虑过枚举,一个案例字典......

Have any of you ran into this type of issue before?你们中有人遇到过这类问题吗? What do you feel might be the best way to approach this from an organizational and ease of use standpoint?从组织和易用性的角度来看,您认为解决此问题的最佳方法是什么?

If the order of functions needed was static obviously it would be easier, but they will change and repeat and change based on need.如果需要的功能顺序是 static 显然会更容易,但它们会根据需要更改,重复和更改。

EDIT: The code is more psudo code, I'm more concerned with how to reference the functions as the list of functions itself may be both long and repetitive.编辑:代码更多是伪代码,我更关心如何引用函数,因为函数列表本身可能既长又重复。

Also fixed the psudo for clarity.为了清楚起见,还修复了 psudo。

I would use a dictionary, and depending on the broader problem at hand I would use either of these approaches:我会使用字典,根据手头更广泛的问题,我会使用以下任何一种方法:

As far as I see if, there is no real reason to intermediate your function objects with an Enum (or similar).据我所知,没有真正的理由将您的 function 对象与 Enum (或类似的)进行中间处理。 That's just adding 1 more layer of complexity that is not needed in Python.这只是增加了一层 Python 中不需要的复杂性。

Approach 1: Flat dictionary w/ tuple keys方法 1:带元组键的平面字典

This approach is ideal if you're only ever interested in selecting the result of 1 func/value pair at a time.如果您只对一次选择 1 个函数/值对的结果感兴趣,那么这种方法是理想的。 If you want to select all outputs with the same function (or likewise, all outputs that share a specific value), I may reach for one of the below approaches.如果您想 select 所有输出具有相同的 function(或同样,所有共享特定值的输出),我可能会采用以下方法之一。

from itertools import product

def directing_function(value_list, func_list):
    transformed = {}
    for func, v in product(func_list, value_list):
        transformed[func, v] = func(v)
    return transformed

def hello(x):
    return x + 1

def world(x):
    return x ** 2

out = directing_function([1, 2, 4, 8], [hello, world])

print(out)
# {
#    (<function hello at 0x7f765c017c10>, 1): 2,
#    (<function hello at 0x7f765c017c10>, 2): 3,
#    ...
#    (<function world at 0x7f765c017160>, 4): 16,
#    (<function world at 0x7f765c017160>, 8): 64
# }

print(out[hello, 2]) # 3
print(out[world, 4]) # 16

Approach 2: Nested dictionary w/ func as outer layer方法 2:以 func 作为外层的嵌套字典

from collections import defaultdict

def directing_function(value_list, func_list):
    transformed = defaultdict(dict)
    for func in func_list:
        transformed[func] = {v: func(v) for v in value_list} 
    return transformed

def hello(x):
    return x + 1

def world(x):
    return x ** 2

out = directing_function([1, 2, 4, 8], [hello, world])

print(out)
# defaultdict(dict,
#             {<function __main__.hello(x)>: {1: 2, 2: 3, 4: 5, 8: 9},
#              <function __main__.world(x)>: {1: 1, 2: 4, 4: 16, 8: 64}})

print(out[world])    # {1: 1, 2: 4, 4: 16, 8: 64}
print(out[world][4]) # 16
print(out[hello][2]) # 3

Approach 2.1: Nested dictionary w/ value as outer layer方法 2.1:以值作为外层的嵌套字典

Pretty much the same as above, but swap the layers of the nested dictionary与上面几乎相同,但交换嵌套字典的层

from collections import defaultdict

def directing_function(value_list, func_list):
    transformed = defaultdict(dict)
    for v in value_list:
        transformed[v] = {func: func(v) for func in func_list} 
    return transformed

Given:鉴于:

l = [1, 2, 3, 4]
f1 = lambda x: x+1
f2 = lambda x: x*2
f3 = lambda x: x**3
funcs = [f1, f2, f3]

Doing:正在做:

# Iterate through your functions.
    # Use map to easily apply a function 
    # to every element in a list.

for f in funcs:
    l = map(f, l)

l = list(l)
print(l)
 
# Output:
[64, 216, 512, 1000]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM