如何使用基本工具和实用程序将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}
]}
答案 0 :(得分:3)
只是做:
import json
myjson = json.dumps(mylst)
答案 1 :(得分: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)