python - if语句可以是变量吗?

标签 python algorithm loops if-statement conditional-statements

为标题道歉,我不知道我正在尝试做的事情的术语。

我有一个包含一系列嵌套循环的函数,它运行了很多次。循环有很多关于它们是继续还是重新开始的条件。

有时,调用此函数需要满足某些罕见的条件。但是,要检查它们,我需要在我的代码中添加更多 if 语句。

这是我正在谈论的一个例子,我函数中的一些循环:

while 1:

    for node in currset:

        for (sum1, weight1, l1, r1) in pathdict[(0, node)]:  
            if sum1 & sum != sum1 or r1 == left or r1 == right or l1 == left:
                continue

            if level < connectright:
                if l1 == right:
                    continue
            elif l1 != right:
                    continue

            for (sum2, weight2, l2, r2) in pathdict[(level, r1)]:
                if sum2&sum != sum2:
                    continue
                if l2 != left or l2 == right:
                    continue
                andsum = sum1&sum2

                if andsum != r1:
                    continue  

现在,如果我有一个特殊情况,其中许多条件不再适用,我该怎么办?我现在的看法是,我可以 1.) 编写一个全新的函数,2.) 重写整个代码块并将其放在另一个 if 语句之后,3.) 添加一系列甚至更多的条件,或者 4. )编写一个函数来检查我在给定步骤的条件。

前两个选项看起来很笨重,但至少我的代码运行速度很快。第三个看起来很糟糕,因为代码变得难以阅读,而且速度变慢。第 4 个,我不确定,但似乎多次调用这些函数会减慢我的循环。

所以我的问题是,有没有什么方法可以编写“条件 if 语句”,根据传递给函数的变量,我可以这样做:

if variable_passed == thing#1:
     my_if_statement = (if level < connectright:  continue)
else:
     my_if_statement = (if level > connectright:  continue)

这样我就可以在开始循环之前轻松地更改函数开始时的条件。

最佳答案

您可以创建函数列表,这些函数都采用相同的参数并使用参数的子集计算条件。

除非你有很多,或者正在创造条件,否则我觉得有点多。

一个例子:

outer = [  # lambda parameters are all that could be used in any expression at point of call
            (lambda sum, sum1, weight1, l1, r1, left, right, level, connectright:
                sum1 & sum != sum1 or r1 == left or r1 == right or l1 == left),
            (lambda sum, sum1, weight1, l1, r1, left, right, level, connectright:
                level < connectright),
            (lambda sum, sum1, weight1, l1, r1, left, right, level, connectright: 
                l1 != right),
        # ... add extra outer loop conditions as necessary
        ]
inner1 = [  # lambda parameters are all that could be used in any expression at point of call
            (lambda sum2, weight2, l2, r2, sum, sum1, weight1, l1, r1, left, right, level, connectright: 
                sum2&sum != sum2),
            (lambda sum2, weight2, l2, r2, sum, sum1, weight1, l1, r1, left, right, level, connectright: 
                l2 != left or l2 == right),
        # ... add extra inner loop conditions as necessary
        ]
inner2 = [  # lambda parameters are all that could be used in any expression at point of call
            (lambda andsum, sum2, weight2, l2, r2, sum, sum1, weight1, l1, r1, left, right, level, connectright: 
                andsum != r1),
        # ... add extra inner loop #2 conditions as necessary
        ]

while True:
    for node in currset:
        for (sum1, weight1, l1, r1) in pathdict[(0, node)]:  
            if any(f(sum, sum1, weight1, l1, r1, left, right, level, connectright)
                    for f in outer):
                continue
            for (sum2, weight2, l2, r2) in pathdict[(level, r1)]:
                if any(f(sum2, weight2, l2, r2, sum, sum1, weight1, l1, r1, left, right, level, connectright)
                        for f in inner1):
                    continue
                andsum = sum1&sum2
                if any(f(andsum, sum2, weight2, l2, r2, sum, sum1, weight1, l1, r1, left, right, level, connectright)
                        for f in inner2):
                    continue

关于python - if语句可以是变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48891471/

相关文章:

python - 计算周期性二维晶格中的成对空间距离

python - 如果存在某些值,如何重新分配具有重复值的列的值?

c++ - 试图在 C++ 中反转字符串但返回相同的字符串

algorithm - 获取二进制字符串中零个数的最佳方法

vba - 循环浏览工作表

python - 调用随机 x 和 y 后,Bokeh Graph Jupyter 上没有显示任何数据

algorithm - 查找 1's and 0' 字符串中第一次出现的 0

c++ - 检查彩虹数组(检查数组的反向样式是否与自身匹配)

java - java中使用字符串进行输入验证的循环

python - 使用 swig 继承 c++ 时出错