zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python - 字典之间的格式转换参考

Python转换 格式 之间 字典 参考
2023-09-27 14:29:10 时间
# coding=utf-8
result = [
    {"type": "MYSQL", "host": "going", "count": "100"},
    {"type": "MYSQL", "host": "success", "count": "200"},
    {"type": "MYSQL", "host": "failed", "count": "300"},

    {"type": "redis", "host": "going", "count": "400"},
    {"type": "redis", "host": "success", "count": "500"},
    {"type": "redis", "host": "failed", "count": "600"},
]

def make_data(result):
    ret = []
    type_list = []
    for t in result:
        type = t.get("type")
        if type not in type_list:
            type_list.append(type)
        print '\n'
    print "type_list = ", type_list

    for i in type_list:
        a = dict()
        for t in result:
            if t.get("type") == i:
                a.update({t.get('host'): t.get('count')})
        a.update({"type": i})
        print 'a = ', a
        ret.append(a)
    return ret


if __name__ == "__main__":
    # print "ret = ", ret
    ret = make_data(result)
    print 'ret_finally = ', ret