-2

如何仅使用基本工具和实用程序将 python 列表转换为 JSON 格式,并包含特定的“名称”、“组”、“源”、“目标”键名等?构造该格式基本上是很多字符串连接吗?

列表索引表示连通性,例如列表中的索引 0 连接到索引 1、3、4、5、6、7、8、9 和 1 连接到 0、2、3、4、5、6、7、8 , 9

列表:

[[1, 3, 4, 5, 6, 7, 8, 9], [0, 2, 3, 4, 5, 6, 7, 8, 9], [1, 3, 4, 7, 9], [0, 1, 2, 4, 5, 6, 7, 9], [0, 1, 2, 3, 6, 7, 8], [0, 1, 3, 6, 7, 8, 9], [0, 1, 3, 4, 5, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 8, 9], [0, 1, 4, 5, 6, 7, 9], [0, 1, 2, 3, 5, 6, 7, 8]]

变成这样的 JSON 格式?

    {"nodes":
[
{"name":"Zone1","group":0},
{"name":"Zone2","group":1},
{"name":"Zone3","group":2},
{"name":"Zone4","group":3},
{"name":"Zone5","group":4},
{"name":"Zone6","group":5},
{"name":"Zone7","group":6},
{"name":"Zone8","group":7},
{"name":"Zone9","group":8},
{"name":"Zone10","group":9}
],
"links":[
{"source":0,"target":1},
{"source":0,"target":3},
{"source":0,"target":4},
{"source":0,"target":5},
{"source":0,"target":6},
{"source":0,"target":7},
{"source":0,"target":8},
{"source":0,"target":9},
{"source":1,"target":0},
{"source":1,"target":2},
{"source":1,"target":3},
{"source":1,"target":4},
{"source":1,"target":5},
{"source":1,"target":6},
{"source":1,"target":7},
{"source":1,"target":8},
{"source":1,"target":9},
{"source":2,"target":1},
{"source":2,"target":3},
{"source":2,"target":4},
{"source":2,"target":7},
{"source":2,"target":9},
{"source":3,"target":0},
{"source":3,"target":1},
{"source":3,"target":2},
{"source":3,"target":4},
{"source":3,"target":5},
{"source":3,"target":6},
{"source":3,"target":7},
{"source":3,"target":9},
{"source":4,"target":0},
{"source":4,"target":1},
{"source":4,"target":2},
{"source":4,"target":3},
{"source":4,"target":6},
{"source":4,"target":7},
{"source":4,"target":8},
{"source":5,"target":0},
{"source":5,"target":1},
{"source":5,"target":3},
{"source":5,"target":6},
{"source":5,"target":7},
{"source":5,"target":8},
{"source":5,"target":9},
{"source":6,"target":0},
{"source":6,"target":1},
{"source":6,"target":3},
{"source":6,"target":4},
{"source":6,"target":5},
{"source":6,"target":7},
{"source":6,"target":8},
{"source":6,"target":9},
{"source":7,"target":0},
{"source":7,"target":1},
{"source":7,"target":2},
{"source":7,"target":3},
{"source":7,"target":4},
{"source":7,"target":5},
{"source":7,"target":6},
{"source":7,"target":8},
{"source":7,"target":9},
{"source":8,"target":0},
{"source":8,"target":1},
{"source":8,"target":4},
{"source":8,"target":5},
{"source":8,"target":6},
{"source":8,"target":7},
{"source":8,"target":9},
{"source":9,"target":0},
{"source":9,"target":1},
{"source":9,"target":2},
{"source":9,"target":3},
{"source":9,"target":5},
{"source":9,"target":6},
{"source":9,"target":7},
{"source":9,"target":8}
]}
4

3 回答 3

3

做就是了:

import json
myjson = json.dumps(mylst)
于 2012-09-14T18:10:03.293 回答
2

从输入和输出猜测,这可能是您想要的。

import json
def convert(adj_lst):
    links = []
    for i,adj in enumerate(adj_lst):
        links.extend( [{'source':i,'target':n} for n in adj] )
    nodes = [{"name":"Zone%d" % i, "group":i} for i in xrange(len(adj_lst))]
    return {"nodes":nodes, "links":links}

adj_list = [[1, 3, 4, 5, 6, 7, 8, 9], [0, 2, 3, 4, 5, 6, 7, 8, 9], [1, 3, 4, 7, 9], [0, 1, 2, 4, 5, 6, 7, 9], [0, 1, 2, 3, 6, 7, 8], [0, 1, 3, 6, 7, 8, 9], [0, 1, 3, 4, 5, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 8, 9], [0, 1, 4, 5, 6, 7, 9], [0, 1, 2, 3, 5, 6, 7, 8]]
print json.dumps(convert(adj_list), indent=2)
于 2012-09-14T18:24:57.960 回答
1

使用该json模块,您应该可以轻松地对列表进行编码。

正如大卫建议的那样

json.dumps

json.loads从json转向python。Python 提供了出色的文档,在谷歌中输入像“python json”这样的未优化搜索提供了适当的链接作为第一个搜索结果

于 2012-09-14T18:10:02.407 回答