试图简化python中的另一个if-else梯形图

试图简化python中的另一个if-else梯形图,python,if-statement,Python,If Statement,我尝试实现与“”相同的逻辑,这是我早些时候为简化if-else梯形图而请求的。但就我的一生而言,我无法简化这个问题 for each_component in component_objects: if each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'pe' and each_component['name'] == 'PE1

我尝试实现与“”相同的逻辑,这是我早些时候为简化if-else梯形图而请求的。但就我的一生而言,我无法简化这个问题

for each_component in component_objects:
    if each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'pe' and each_component['name'] == 'PE1':
        env_data['PE1_HOSTNAME'] = each_component['hostname'].split('.')[0]

    elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'pe' and each_component['name'] == 'PE2':
        env_data['PE2_HOSTNAME'] = each_component['hostname'].split('.')[0]

    elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'versadirector' and each_component['name'] == 'VersaDirector':
        env_data['VD_HOSTNAME'] = each_component['hostname'].split('.')[0]

    elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'jump' and each_component['name'] == 'Jump':
        jump_ips = json.loads(each_component['ip_addr'])

    elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'versaanalytics' and each_component['name'] == 'VersaAnalytics1':
        analytics_1 = each_component['hostname'].split('.')[0]

    elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'versaanalytics' and each_component['name'] == 'VersaAnalytics2':
        analytics_2 = each_component['hostname'].split('.')[0]

    elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'versaanalytics' and each_component['name'] == 'VersaAnalytics3':
        analytics_3 = each_component['hostname'].split('.')[0]

    elif each_component['region_type'] == deploy_json['env_type'].upper() and each_component['component_type'] == 'versaanalytics' and each_component['name'] == 'VersaAnalytics4':
        analytics_4 = each_component['hostname'].split('.')[0]
我正在尝试生成一个环境数据字典和变量分析。 现在,这个请求的原因是,当运行代码质量工具时,这段代码会发出严重警告,需要进一步简化,以便代码质量检查通过。
有人有什么想法吗?帮助这里

您重复了很多检查。不要那样做。为了简化,只需在重复的
if
语句下嵌套unique
if
s即可:

for each_component in component_objects:
    if each_component['region_type'] == deploy_json['env_type'].upper():

        if each_component['component_type'] == 'pe':
            if each_component['name'] == 'PE1':
                env_data['PE1_HOSTNAME'] = each_component['hostname'].split('.')[0]
            elif each_component['name'] == 'PE2':
                env_data['PE2_HOSTNAME'] = each_component['hostname'].split('.')[0]

        elif each_component['component_type'] == 'versadirector' and each_component['name'] == 'VersaDirector':
            env_data['VD_HOSTNAME'] = each_component['hostname'].split('.')[0]

        elif each_component['component_type'] == 'jump' and each_component['name'] == 'Jump':
            jump_ips = json.loads(each_component['ip_addr'])

        elif each_component['component_type'] == 'versaanalytics':
            if each_component['name'] == 'VersaAnalytics1':
                analytics_1 = each_component['hostname'].split('.')[0]
            elif each_component['name'] == 'VersaAnalytics2':
                analytics_2 = each_component['hostname'].split('.')[0]
            elif each_component['name'] == 'VersaAnalytics3':
                analytics_3 = each_component['hostname'].split('.')[0]
            elif each_component['name'] == 'VersaAnalytics4':
                analytics_4 = each_component['hostname'].split('.')[0]

这几乎是尽可能简化的。如果你想进一步简化,你应该使用函数,因为现在有很多重复的任务。任何重复的内容都可以被函数替换。

您的命名表明数据结构中存在更深层次的问题。你应该更多地使用函数、字典和列表。如果不知道如何使用生成的数据,就很难给出有意义的建议。