如何将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}
]}
区域格式的第一部分是微不足道的,不应该包含在问题中。有关源 - 目标链接的部分需要在for循环中使用if语句。您的实际问题应该是“如何将邻接矩阵转换为链接列表”;除了您使用JSON输出外,JSON与此无关。如果您在使用JSON输出时遇到问题,那么只需使用python'json'模块,就像Google提到的那样。 – ninjagecko
我包括那部分,因为有时候人们会问我包括我试图完成的目的或目标。 – user1518600