python,通过名称组合函数并将它们链接起来调用?

发布于 2024-09-29 05:36:56 字数 2113 浏览 7 评论 0原文

所有,

我在下面感到困惑可能需要您的帮助

假设我有一个基于行可变长度数据矩阵

93  77  96  85
50  69  54  16
39  91  59  38
64  30  18  50
43  9   74  94
44  87  95  89
... 

我想通过上述源使用不同的生成算法生成结果数据,而不同的范围选择算法,请使用下面的代码例如

lsrc = [# above data]

def rangesel_001(lsrc):
    tmp = []
    for i in len(lsrc):
       if i % 2 = 0:
          tmp.append(lsrc[i])
    return tmp

def algo_001(lsrc):
    tmp = []
        for i in len(lsrc):
            tmp.append([x+1 for x in lsrc[i]]) 
    return tmp

所以我想要的数据是:

dscl = algo_001(rangesel_001(lsrc))

这是我的问题:

1. 假设我有一个可扩展的“rangesel”集,并且“algo”也是可扩展的,看起来像 我想混合它们

rangesel_001()     algo_001()
rangesel_002()     algo_002()
rangesel_003()     algo_003()
…                    ...

,然后立即调用它们以获得我想要的所有结果

rangesel_001 + algo_001
rangesel_001 + algo_002
rangesel_001 + algo_003

rangesel_002 + algo_001
rangesel_002 + algo_002
rangesel_002 + algo_003
...

是否有一种方法可以轻松管理这些函数名称然后将它们组合起来执行?

2. 你可能会注意到“rangesel”和“algo”算法中的不同部分是这里的片段:

if i % 2 = 0:
 and
[x+1 for x in lsrc[i]]

有一种方法可以从函数定义中提取出这些共同部分,我可以给出某种list:

if i % 2 = 0   rangesel_001
if i % 3 = 0   rangesel_002
if i % 4 = 0   rangesel_003
 and
[x+1 for x in lsrc[i]] algo_001
[x/2 for x in lsrc[i]] algo_002

那么我可以获得所有“rangeset”函数和“algo”集?

3.也许稍后我需要这个:

dscl = algo_001(rangesel_001(                             \
                             algo_002(rangesel_002(lsrc)) \  
                            ))

那么,有没有一种轻松的方法可以链接这些“rangesel_002 + algo_001”组合? 例如:假设我已经有了完整的组合

rangesel_001 + algo_001
rangesel_001 + algo_002
rangesel_001 + algo_003

rangesel_002 + algo_001
rangesel_002 + algo_002
rangesel_002 + algo_003

,现在我想随机选择 3 个来链接它们并调用以获取结果列表?

dscl = random_pick(3, combination_list, lsrc)

谢谢!

All,

I have confuse below may need your help

suppose I have a row based variable length data matrix

93  77  96  85
50  69  54  16
39  91  59  38
64  30  18  50
43  9   74  94
44  87  95  89
... 

I want to generate result data via above source with different generating algorithms while the different range selection algorithms, take below code for example

lsrc = [# above data]

def rangesel_001(lsrc):
    tmp = []
    for i in len(lsrc):
       if i % 2 = 0:
          tmp.append(lsrc[i])
    return tmp

def algo_001(lsrc):
    tmp = []
        for i in len(lsrc):
            tmp.append([x+1 for x in lsrc[i]]) 
    return tmp

So the data I want is:

dscl = algo_001(rangesel_001(lsrc))

Here comes is my questions:

1. suppose I have an extendable "rangesel" set and the "algo" is also extendable looks like this

rangesel_001()     algo_001()
rangesel_002()     algo_002()
rangesel_003()     algo_003()
…                    ...

I want to mix them, then invoke them in-a-go to get all the result what I want

rangesel_001 + algo_001
rangesel_001 + algo_002
rangesel_001 + algo_003

rangesel_002 + algo_001
rangesel_002 + algo_002
rangesel_002 + algo_003
...

is there a way easy to manager those function names then combine them to execute?

2. you may noticed the different part in “rangesel”s and “algo”s algorithm is snippet here:

if i % 2 = 0:
 and
[x+1 for x in lsrc[i]]

It there a way to exact those common part out from function definitions and I can just give somekind a list:

if i % 2 = 0   rangesel_001
if i % 3 = 0   rangesel_002
if i % 4 = 0   rangesel_003
 and
[x+1 for x in lsrc[i]] algo_001
[x/2 for x in lsrc[i]] algo_002

then I can get all the “rangeset” functions and "algo" sets?

3. maybe later I need this:

dscl = algo_001(rangesel_001(                             \
                             algo_002(rangesel_002(lsrc)) \  
                            ))

so, is there a painfulless way I can chain those "rangesel_002 + algo_001" combinations?
for example: suppose I already have the full combinations

rangesel_001 + algo_001
rangesel_001 + algo_002
rangesel_001 + algo_003

rangesel_002 + algo_001
rangesel_002 + algo_002
rangesel_002 + algo_003

now I want to pick random 3 to chain them and invoke to get result list?

dscl = random_pick(3, combination_list, lsrc)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿得七秒忆 2024-10-06 05:36:56

对于第一个问题,您可以像这样定义一个函数组合操作:

def compose(f, g):
    return lambda *x: f(g(*x))

然后,您可以:

ra = compose(rangeset_001, algo_001)
ra(lsrc)

如果您创建这样的函数列表:

rangesets = [rangeset_001, rangeset_002, rangeset_003]

那么您可以迭代:

for r in rangesets:
    ra = compose(r, algo_001)
    ra(lsrc)

将这个想法扩展到 algo_xxx 函数是也有可能。

For your first question, you can define a function composition operation like this:

def compose(f, g):
    return lambda *x: f(g(*x))

Then, you can:

ra = compose(rangeset_001, algo_001)
ra(lsrc)

If you make lists of functions like this:

rangesets = [rangeset_001, rangeset_002, rangeset_003]

then you can iterate:

for r in rangesets:
    ra = compose(r, algo_001)
    ra(lsrc)

Expansion of this idea to the algo_xxx functions is also possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文