码迷,mamicode.com
首页 > 其他好文 > 详细

20201019_02_简化代码

时间:2020-10-19 23:00:56      阅读:28      评论:0      收藏:0      [点我收藏+]

标签:for   怎么   简化   work   文档   ksh   怎么办   mat   eve   

案例:请简化以下代码
m = 0 
for i in range(3,length_a1):
    # 从设备侧文档 判断层级,验证层级,并打印层级关系
    if table1.col(1)[i].value == ‘‘:                                    
        print(table1.col(3)[i].value + ":第一层")             
        m += 1
        worksheet1.write(m,0,table1.col(3)[i].value)            
        worksheet1.write(m,1,‘第 1 层‘)
        worksheet1.write(m,2,‘【‘ + table1.col(3)[i].value + ‘】‘ + ‘是顶层!‘)
        if table1.col(2)[i].value in F_Mo:
            # print(table1.col(3)[i].value + ":第一层 存在 子MO")
            for i01 in range(3,length_a1):
                if table1.col(2)[i].value == table1.col(1)[i01].value:
                    print(table1.col(3)[i01].value + ":第二层 " + "上一层级为:" + table1.col(3)[i].value)
                    m += 1
                    worksheet1.write(m,0,table1.col(3)[i01].value)
                    worksheet1.write(m,1,‘第 2 层‘)
                    worksheet1.write(m,2,table1.col(3)[i].value)
                    if table1.col(2)[i01].value in F_Mo:
                        # print(table1.col(3)[i01].value + ":第二层 存在 子MO")
                        for i02 in range(3,length_a1):
                            if table1.col(2)[i01].value == table1.col(1)[i02].value:
                                print(table1.col(3)[i02].value + ":第三层" + "上一层级为:" + table1.col(3)[i01].value)
                                m += 1
                                worksheet1.write(m,0,table1.col(3)[i02].value)
                                worksheet1.write(m,1,‘第 3 层‘)
                                worksheet1.write(m,2,table1.col(3)[i01].value)
                                if table1.col(2)[i02].value in F_Mo:
                                    #print(table1.col(3)[i02].value + ":第三层 存在 子MO")
                                    for i03 in range(3,length_a1):
                                        if table1.col(2)[i02].value == table1.col(1)[i03].value:
                                            print(table1.col(3)[i03].value + ":第四层" + "上一层级为:" + table1.col(3)[i02].value)
                                            m += 1
                                            worksheet1.write(m,0,table1.col(3)[i03].value)
                                            worksheet1.write(m,1,‘第 4 层‘)
                                            worksheet1.write(m,2,table1.col(3)[i02].value)
 
.......如果有 几十层 怎么办?
 
方案1:
data = {}
for i in range(3,length_a1):
    parent, name, zh = table1.col(1)[i].value, table1.col(2)[i].value, table1.col(3)[i].value
    data[name] = {‘parent‘: parent, ‘name‘: name, ‘zh‘: zh, ‘level‘: 0}
while True:
    mod = False
    for v in data.values():
        if v[‘level‘] == 0:
            if v[‘parent‘] == ‘‘:
                v[‘level‘] = 1
                mod = True
            elif data[v[‘parent‘]][‘level‘] != 0:
                v[‘level‘] = data[v[‘parent‘]][‘level‘] + 1
                mod = True
    if not mod: break

print(data)

m = 0
for v in data.values():
    m += 1
    worksheet1.write(m, 0, v[‘zh‘])
    worksheet1.write(m, 1, ‘第 {} 层‘.format(v[‘level‘]))
    if v[‘level‘] == 1:
        worksheet1.write(m, 2, ‘【{}】是顶层!‘.format(v[‘zh‘]))
    else:
        worksheet1.write(m, 2, ‘{}‘.format(data[v[‘parent‘]][‘zh‘]))

20201019_02_简化代码

标签:for   怎么   简化   work   文档   ksh   怎么办   mat   eve   

原文地址:https://www.cnblogs.com/azxsdcv/p/13841935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!