python - 处理大型 if/else 的最佳方法

标签 python time-complexity readability

我有一个非常大的 if/else block ,它将对字节数组执行操作。 从技术上讲,id 可以是从 0x001 到 0xFFF 之间的任何数字,但它们是众所周知的。每种情况都是唯一的,并将对数组中的不同字节执行计算。消息以大约 0.5ms - 1ms 的速率来自队列,并通过此方法进行处理

虽然大型 if/else 有效,但我看到的最大问题是,如果在此示例中 id 为 1222,则我们必须命中之前的所有 if。此外,随着添加更多消息,时间复杂度也会增加,并且可能会变得非常大。

def calcCanMessage(self):
    while: True
        #get msg - simplified for example
        msgId = msg.id
        msgData = msg.data
        if msgId == 324:
            #do something with msgData
            continue
        elif msgId == 211:
            continue
        elif msgId == 322:
            continue
        elif msgId == 321:
            continue
        elif msgId == 320:
            continue
        elif msgId == 342:
            continue
        elif msgId == 338:
            continue
        elif msgId == 24:
            continue
        elif msgId == 212:
            continue
        elif msgId == 323:
            continue
        elif msgId == 210:
            continue
        elif msgId == 209:
            continue
        elif msgId == 208:
            continue
        elif msgId == 642:
            continue
        elif msgId == 880:
            continue
        elif msgId == 1088:
            continue
        elif msgId == 865:
            continue
        elif msgId == 864:
            continue
        elif msgId == 882:
            continue
        elif msgId == 1595:
            continue
        elif msgId == 1090:
            continue
        elif msgId == 885:
            continue
        elif msgId == 884:
            continue
        elif msgId == 1224:
            continue
        elif msgId == 1761:
            continue
        elif msgId == 1762:
            continue
        elif msgId == 1245:
            continue
        elif msgId == 1219:
            continue
        elif msgId == 1217:
            continue
        elif msgId == 1222:
            continue
        else:
            print('unknown ID: ', msgId)

最佳答案

首先,定义使用 msgData 执行(不同)操作的函数。函数体将是您当前在 if..else 语句的每个 block 中执行的操作。

def msgId_324(msgData):
    #do something with msgData

def msgId_211(msgData):
    #do something else with msgData

# more functions to handle msgData

然后定义一个字典,将所有消息 ID 值与其相应的函数配对。

msgId_dict = {324: msgId_324, 
            211: msgId_211,
            # more msgId: msg_function pairs
            }

现在您可以用字典查找替换 if 语句,无论有多少消息值,这都会快得多,并调用返回的函数。

msgId = msg.id
msgData = msg.data

if msgId in msgId_dict:
    msg_function = msgId_dict[msgId]
    msg_function(msgData)
else:
    # optional else case to handle unrecognized message IDs

关于python - 处理大型 if/else 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69707989/

相关文章:

python - for循环中嵌套if语句中的特定缩进错误

python - len() 关于集合和列表的复杂性

java - 给 Java 程序员的 Python 可读性提示

python - 我无法完全理解这行代码

jquery - 我可以通过像在 CSS 中那样对它们进行分组来将相同的样式应用于 jQuery 中的两个元素吗?

python - 如何实例化现有的 QGIS 插件工具栏

python - Python 函数中的类型错误

python - 如何填充 matplotlib 直方图的中心 95% 置信区间?

algorithm - 如何确定这个算法的时间复杂度?

algorithm - 就时间复杂度而言,使用最佳方法从字符矩阵形成字符串的方法有多少?