如何检查 "if condition" 并动态分配它? (Python)

How to check the "if condition" and assign it on the fly? (Python)

说,我想检查我的整数是否在第一个函数的 return 中,如果不在,则去检查第二个函数,依此类推。具有这些功能

import time

def a_very_expensive_function_1():
    time.sleep(1)
    return [0,1,2]

def a_very_expensive_function_2():
    time.sleep(1)
    return [3,4,5]

# and maybe more functions
# def a_very_expensive_function_3():
#     time.sleep(1)
#     return [6,7,8]
# ...

my_int = 1


if my_int in a_very_expensive_function_1():
    result = a_very_expensive_function_1() + [10] # I have to call`a_very_expensive_function_1()` 2nd times here
elif my_int in a_very_expensive_function_2():
    result = a_very_expensive_function_2() + [10]

您可以注意到,使用这种方法,我必须调用第一个函数 2 次,但我不必 运行 第二个函数,因为第一个函数为 True,(否则我必须像第一个函数一样重复第二个函数 2 次。)

现在我想找到一个替代方法,通过这样做来避免调用第一个函数 2 次:

my_int = 1

first_list = a_very_expensive_function_1()
second_list = a_very_expensive_function_2()

if my_int in first_list:
    result = first_list + [10] 
elif my_int in second_list:
    result = second_list + [10]

现在我不必再调用第一个函数 2 次了,但现在我必须在检查条件之前调用第二个函数(依此类推),我想如果我有更多的话,它会严重扩展超过 2 个函数。

是否有解决这些问题的最佳方法?

PS。我不确定,但我觉得 walrus operator(:=) 可以解决这个问题?但由于它是相对较新的功能,而且我的开发环境 (Google Colab) 仍在使用 Python 3.6,我更喜欢更原始和传统的方法来解决这个问题。

编辑:对于任何好奇的人,在 Python 3.8+ 上,这里是如何使用海象运算符动态分配。同样,现在我更喜欢更原始的方法,而不是使用人们仍然不习惯的功能。

import time

def a_very_expensive_function_1():
    time.sleep(1)
    return [0,1,2]

def a_very_expensive_function_2():
    time.sleep(1)
    return [3,4,5]

my_int = 1

if my_int in (result := a_very_expensive_function_1()):
    result += [10] 
elif my_int in (result := a_very_expensive_function_2()):
    result += [10] 

print(result)
# [0, 1, 2, 10]

EDIT2:为超过 2 个函数添加示例

你可以这样做:

my_int = 1

first_list = a_very_expensive_function_1()

if my_int in first_list:
    result = first_list + [10] 
else:
    second_list = a_very_expensive_function_2()
    if my_int in second_list:
        result = second_list + [10]

呃,你就不能做这样的事情吗?

my_int = 1

first_list = a_very_expensive_function_1()   
if my_int in first_list:
    result = first_list + [10] 
else:
    second_list = a_very_expensive_function_2()
    if my_int in second_list:
        result = second_list + [10]

如果您要调用两个以上的昂贵函数:

very_expensive_functions = [
    a_very_expensive_function_1,
    a_very_expensive_function_2,
    a_very_expensive_function_3,
    a_very_expensive_function_4,
    a_very_expensive_function_5
]

my_int = 1

for f in very_expensive_functions:
    f_result = f()
    if my_int in f_result:
        return f_result + [10]

Try it online!

import time

def a_very_expensive_function_1():
    time.sleep(1)
    return [0,1,2]

def a_very_expensive_function_2():
    time.sleep(1)
    return [3,4,5]

def find(n, funcs):
    for e in funcs:
        r = e()
        if n in r:
            return r
    assert False, 'Not found!'

my_int = 1

print(find(my_int, [
    a_very_expensive_function_1,
    a_very_expensive_function_2,
]) + [10])

输出:

[0, 1, 2, 10]

如果您对创建单独的辅助函数 (+ return) 感到不自在,那么您可以使用内联 for 循环和 break:[=19= 做完全相同的事情]

Try it online!

import time

def a_very_expensive_function_1():
    time.sleep(1)
    return [0,1,2]

def a_very_expensive_function_2():
    time.sleep(1)
    return [3,4,5]


my_int = 1

for e in [
    a_very_expensive_function_1,
    a_very_expensive_function_2,
]:
    r = e()
    if my_int in r:
        break
else:
    assert False, 'Not found!'

print(r + [10])

输出:

[0, 1, 2, 10]