zoukankan      html  css  js  c++  java
  • Python switch-case语句的实现 -- 字典模拟实现

    static void
    print_asru_status(int status, char *label)
    {
        char *msg = NULL;
    
        switch (status) {
        case 0:
            msg = dgettext("FMD", "ok and in service");
            break;
        case FM_SUSPECT_DEGRADED:
            msg = dgettext("FMD", "service degraded, "
                "but associated components no longer faulty");
            break;
        case FM_SUSPECT_FAULTY | FM_SUSPECT_DEGRADED:
            msg = dgettext("FMD", "faulted but still "
                "providing degraded service");
            break;
        case FM_SUSPECT_FAULTY:
            msg = dgettext("FMD", "faulted but still in service");
            break;
        case FM_SUSPECT_UNUSABLE:
            msg = dgettext("FMD", "out of service, "
                "but associated components no longer faulty");
            break;
        case FM_SUSPECT_FAULTY | FM_SUSPECT_UNUSABLE:
            msg = dgettext("FMD", "faulted and taken out of service");
            break;
        default:
            break;
        }
        if (msg) {
            (void) printf("%s     %s
    ", label, msg);
        }
    }

    以上代码是我做项目中遇到的一段代码,要求用Python语言实现,实现代码如下所示:

    def print_asru_status(status, label):
        dic = {0:"ok and in service",
            FM_SUSPECT_DEGRADED:"service degraded, but associated components no longer faulty",
            FM_SUSPECT_FAULTY | FM_SUSPECT_DEGRADED:"service degraded, but associated components no longer faulty",
            FM_SUSPECT_FAULTY:"faulted but still in service",
            FM_SUSPECT_UNUSABLE:"out of service, but associated components no longer faulty",
            FM_SUSPECT_FAULTY | FM_SUSPECT_UNUSABLE:"faulted and taken out of service"}
        if status not in dic:
            msg = ""
        else:
            msg = dic[status]
        if msg :
            print(label,"     ", msg)

    学习文章:http://www.cnblogs.com/russellluo/p/3299725.html

  • 相关阅读:
    二分类实现多分类
    目标检测(三) Fast R-CNN
    目标检测(二) SPPNet
    目标检测(一) R-CNN
    超参数调优
    支持向量机 SVM
    LDA 线性判别分析
    分类
    特征选择
    集成学习-Adaboost 参数选择
  • 原文地址:https://www.cnblogs.com/fendou-999/p/3534839.html
Copyright © 2011-2022 走看看