使用类似逻辑调用多个函数的最佳实践

发布于 2025-01-14 04:41:27 字数 1444 浏览 2 评论 0原文

当您有 10-100 个采用相同参数并返回相同结果的函数时,调用它们的最佳实践是什么?写 f1(), f2() 100 次?将它们放在 dic 或 list 上并迭代?创建一个类,但仍然如何从那里调用他们?

我有 10 个函数,名称末尾带有数字。他们获取一个列表并返回一个新列表。

def func_1(lst):
    logic
    return new_lst

def func_2(lst):
    logic
    return new_lst
...
def func_10(lst):
    logic
    return new_lst

ls = [SOME DATA] # The list with some data that must be handled by 10 functions

# THE QUESTION IS ABOUT THIS: how to make it more elegant?
lst_1, lst_2, lst_3, lst_4, lst_5, lst_6, lst_7, lst_8, lst_9, lst_10 = func_1(ls) + func_2(ls) + func_3(ls) + func_4(ls) + func_5(ls) + func_6(ls) + func_7(ls) + func_8(ls) + func_9(ls) + func_10(ls) 

我有一个循环解决方案,但不知道它是否好。

ls = 'ABCD'
def func_1(lst):
    new_lst = []
    for s in lst:
        msg = 'Func1 ' + str(s)
        new_lst.append(msg)
    return new_lst
def func_2(lst):
    new_lst = []
    for s in lst:
        msg = 'Func2 ' + str(s)
        new_lst.append(msg)
    return new_lst
# ...
def func_10(lst):
    new_lst = []
    for s in lst:
        msg = 'Func10 ' + str(s)
        new_lst.append(msg)
    return new_lst
x = 10
name_lst = 'lst'
name_func = 'func'
list_to_proceed = 'ls'
dic = {}
for i in range(x):
    lst_name = f'{name_lst}_{str(i+1)}'
    func_name = f'{name_func}_{str(i+1)}({list_to_proceed})'
    dic[lst_name] = eval(func_name)

我的总体目标是将每个函数的结果(列表)移交给另一个函数,该函数应根据每个列表的内容继续列表。另外,也许可以用一些装饰器和生成器来完成,但无法弄清楚

When you have 10-100 functions that take the same parameter and return the same result, which is the best practice to call them? Write f1(), f2() 100 times? Put them on dic or list and iterate? Make a class, but still how to call them from there?

I have 10 functions with number at the end of the name. They take a list and return a new list.

def func_1(lst):
    logic
    return new_lst

def func_2(lst):
    logic
    return new_lst
...
def func_10(lst):
    logic
    return new_lst

ls = [SOME DATA] # The list with some data that must be handled by 10 functions

# THE QUESTION IS ABOUT THIS: how to make it more elegant?
lst_1, lst_2, lst_3, lst_4, lst_5, lst_6, lst_7, lst_8, lst_9, lst_10 = func_1(ls) + func_2(ls) + func_3(ls) + func_4(ls) + func_5(ls) + func_6(ls) + func_7(ls) + func_8(ls) + func_9(ls) + func_10(ls) 

I have a loop solution, but don't know if it's good.

ls = 'ABCD'
def func_1(lst):
    new_lst = []
    for s in lst:
        msg = 'Func1 ' + str(s)
        new_lst.append(msg)
    return new_lst
def func_2(lst):
    new_lst = []
    for s in lst:
        msg = 'Func2 ' + str(s)
        new_lst.append(msg)
    return new_lst
# ...
def func_10(lst):
    new_lst = []
    for s in lst:
        msg = 'Func10 ' + str(s)
        new_lst.append(msg)
    return new_lst
x = 10
name_lst = 'lst'
name_func = 'func'
list_to_proceed = 'ls'
dic = {}
for i in range(x):
    lst_name = f'{name_lst}_{str(i+1)}'
    func_name = f'{name_func}_{str(i+1)}({list_to_proceed})'
    dic[lst_name] = eval(func_name)

My overall goal is to hand over each function's result (a list) to another function which, based on the content of each of list, should proceed lists. Also, maybe it could be done with some decorators and generators, but couldn't figure it out

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文