• 示例:python环境下字典和列表重组的一个小例子


    #发文福利#

    1. #!/usr/bin/env python
    2. # -*- coding:utf-8 -*-
    3. result = {}
    4. iplist = ['1.1.1.1', '1.1.1.2', '1.1.1.3', '1.1.1.4', '1.1.1.5', '1.1.1.6']
    5. for i in iplist:
    6. tmp1 = int(i.split(".")[3])
    7. jmx_state = 'online' if tmp1 % 2 == 1 else 'offline'
    8. address = 'shanghai' if tmp1 % 2 == 1 else 'hangzhou'
    9. result[i] = {'jmx_state': jmx_state, 'address': address, 'cmdb_state': '', 'compare': ''}
    10. print result
    11. #{'jmx_state': 'offline', 'address': 'hangzhou'}
    12. cmdb_state = 'haha'
    13. result['1.1.1.2']['cmdb_state'] = cmdb_state
    14. if result['1.1.1.2']['cmdb_state'] == 'hehe':
    15. result['1.1.1.2'].update({'compare': 'success'})
    16. else:
    17. result['1.1.1.2'].update({'compare': 'not ok'})
    18. print result['1.1.1.2'] # {'jmx_state': 'offline', 'compare': 'not ok', 'cmdb_state': 'haha', 'address': 'hangzhou'}
    19. for k,v in result.items():
    20. var1 = [k,v['jmx_state'],v['address'],v['cmdb_state'],v['compare']]
    21. print ','.join(var1)

    调试结果如下

    {'1.1.1.4': {'jmx_state': 'offline', 'compare': '', 'cmdb_state': '', 'address': 'hangzhou'}, '1.1.1.5': {'jmx_state': 'online', 'compare': '', 'cmdb_state': '', 'address': 'shanghai'}, '1.1.1.6': {'jmx_state': 'offline', 'compare': '', 'cmdb_state': '', 'address': 'hangzhou'}, '1.1.1.1': {'jmx_state': 'online', 'compare': '', 'cmdb_state': '', 'address': 'shanghai'}, '1.1.1.2': {'jmx_state': 'offline', 'compare': '', 'cmdb_state': '', 'address': 'hangzhou'}, '1.1.1.3': {'jmx_state': 'online', 'compare': '', 'cmdb_state': '', 'address': 'shanghai'}}
    {'jmx_state': 'offline', 'compare': 'not ok', 'cmdb_state': 'haha', 'address': 'hangzhou'}
    1.1.1.4,offline,hangzhou,,
    1.1.1.5,online,shanghai,,
    1.1.1.6,offline,hangzhou,,
    1.1.1.1,online,shanghai,,
    1.1.1.2,offline,hangzhou,haha,not ok
    1.1.1.3,online,shanghai,,
    

    听哥一句劝,不要做运维 

  • 相关阅读:
    [JAVAee]Spring拦截器
    单片机是不是嵌入式呢,老生常谈了
    JVM 垃圾回收算法详解
    React Router 路由守卫
    浅析基于视频图像的智能分析网关AI算法及场景应用
    Merbridge 入选 eBPF 全景图
    TP6中Field使用
    Hexagon_V65_Programmers_Reference_Manual (51)
    Word文档中书签使用注意事项
    SQL数据分析:sqlzoo官网学习select,where,order by,limit,聚合函数,having,常用函数,窗口函数,表链接,子查询
  • 原文地址:https://blog.csdn.net/xoofly/article/details/132725488