How to go through each sub-grid 3x3 using for loop? - python

I have been working on sudoku. The size of the original grid is 9x9 (a list containing 9 lists, each of which is a row). I need to check whether the digits only occur once per 3x3 sub-grid. In order to do that I have to go through each sub-grid using for loop (I think). So, I spent quite some time trying to do that, but I cannot seem to understand how exactly do it using for loop.
example_of_full_grid = [[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 0, 3, 4, 9],
[1, 0, 0, 3, 4, 2, 5, 6, 0],
[8, 5, 9, 7, 6, 1, 0, 2, 0],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 0, 1, 5, 3, 7, 2, 1, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 0, 0, 4, 8, 1, 1, 7, 9]]

Is it possible to use numpy for you?
The code below loops over all 9 subgrids.
import numpy as np
grid = np.array([[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 0, 3, 4, 9],
[1, 0, 0, 3, 4, 2, 5, 6, 0],
[8, 5, 9, 7, 6, 1, 0, 2, 0],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 0, 1, 5, 3, 7, 2, 1, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 0, 0, 4, 8, 1, 1, 7, 9]])
for i in range(0,9,3):
for j in range(0,9,3):
print(grid[i:i+3,j:j+3])
This has to be changed for a list. See below:
subgrid = []
for i in range(0,9,3):
row_3x3 = []
for j in range(0,9):
row_3x3.append(example_of_full_grid[j][i:i+3])
for j in range(0,9,3):
subgrid.append(row_3x3[j:j+3])
print(row_3x3[j:j+3])

Related

When appended a copy of list into another list, after altering the original list it also gets altered in another list [duplicate]

This question already has answers here:
How to deep copy a list?
(10 answers)
Closed 1 year ago.
k=[
[7, 1, 3, 6, 8, 5, 5, 6, 4],
[7, 2, 6, 2, 2, 8, 3, 9, 6],
[3, 3, 8, 6, 1, 3, 4, 5, 9],
[4, 5, 9, 8, 6, 6, 1, 3, 4],
[2, 8, 1, 4, 8, 6, 9, 5, 1],
[4, 7, 8, 6, 1, 8, 5, 8, 4],
[6, 7, 6, 4, 8, 6, 6, 7, 2],
[9, 8, 6, 3, 8, 8, 5, 5, 9],
[9, 5, 7, 5, 1, 1, 8, 6, 5]
]
a=[]
c=0
def foo():
global a
global k
global c
a.append(k.copy())
print(a)
for i in range(9):
for j in range(9):
k[i][j]=1
print(a)
foo()
Expected Output:
[[[7, 1, 3, 6, 8, 5, 5, 6, 4], [7, 2, 6, 2, 2, 8, 3, 9, 6], [3, 3, 8, 6, 1, 3, 4, 5, 9], [4, 5, 9, 8, 6, 6, 1, 3, 4], [2, 8, 1, 4, 8, 6, 9, 5, 1], [4, 7, 8, 6, 1, 8, 5, 8, 4], [6, 7, 6, 4, 8, 6, 6, 7, 2], [9, 8, 6, 3, 8, 8, 5, 5, 9], [9, 5, 7, 5, 1, 1, 8, 6, 5]]]
[[[7, 1, 3, 6, 8, 5, 5, 6, 4], [7, 2, 6, 2, 2, 8, 3, 9, 6], [3, 3, 8, 6, 1, 3, 4, 5, 9], [4, 5, 9, 8, 6, 6, 1, 3, 4], [2, 8, 1, 4, 8, 6, 9, 5, 1], [4, 7, 8, 6, 1, 8, 5, 8, 4], [6, 7, 6, 4, 8, 6, 6, 7, 2], [9, 8, 6, 3, 8, 8, 5, 5, 9], [9, 5, 7, 5, 1, 1, 8, 6, 5]]]
Generated Output:
[[[7, 1, 3, 6, 8, 5, 5, 6, 4], [7, 2, 6, 2, 2, 8, 3, 9, 6], [3, 3, 8, 6, 1, 3, 4, 5, 9], [4, 5, 9, 8, 6, 6, 1, 3, 4], [2, 8, 1, 4, 8, 6, 9, 5, 1], [4, 7, 8, 6, 1, 8, 5, 8, 4], [6, 7, 6, 4, 8, 6, 6, 7, 2], [9, 8, 6, 3, 8, 8, 5, 5, 9], [9, 5, 7, 5, 1, 1, 8, 6, 5]]]
[[[1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1]]]
With copy() you perform a shallow copy.
You need a deep copy. See Docs for more information
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
from copy import deepcopy
a.append(deepcopy(k))
print(a)
for i in range(9):
for j in range(9):
k[i][j]=1
print(a)
.copy method of list is shallow, you need to use copy.deepcopy if you want to get totally independent copy, consider following example
import copy
k1 = [[1,2],[3,4]]
k2 = k1.copy()
k3 = copy.deepcopy(k1)
k1[0][0] = 0
print(k2)
print(k3)
output
[[0, 2], [3, 4]]
[[1, 2], [3, 4]]
Using .copy is fine if you are working with flat list of immutable objects, you have list of list which are mutable.

How to replace specific values in PyTorch tensor along diagonal?

For example, there is a PyTorch matrix A:
A = tensor([[3,2,1],[1,0,2],[2,2,0]])
I need to replace 0 with 1 on the diagonal, so the result should be:
tensor([[3,2,1],[1,1,2],[2,2,1]])
You can use torch's inbuilt diagonal functions to replace diagonal elements like so:
mask = A.diagonal() == 0
A += torch.diag(mask)
>>> A
tensor([[3, 2, 1],
[1, 1, 2],
[2, 2, 1]])
If you want to replace 0's with another value, change mask to mask * replace_value.
You can use vector indexing to extract the diagonal, process it, and then put it back into your original matrix:
N=10
a = torch.randint(0,N,[N,N])
#tensor([[0, 9, 6, 6, 9, 9, 3, 1, 8, 4],
# [8, 1, 6, 8, 5, 8, 7, 8, 1, 4],
# [1, 9, 8, 4, 7, 0, 2, 9, 6, 2],
# [9, 5, 9, 6, 7, 1, 4, 0, 2, 6],
# [1, 2, 8, 0, 9, 0, 4, 3, 9, 9],
# [1, 4, 6, 9, 6, 5, 1, 2, 0, 7],
# [4, 8, 1, 3, 1, 6, 1, 3, 5, 6],
# [3, 8, 9, 9, 1, 3, 0, 9, 6, 6],
# [7, 4, 3, 0, 3, 5, 6, 6, 9, 2],
# [3, 1, 0, 8, 3, 5, 6, 6, 5, 5]])
diag = a[range(N),range(N)] #index (1,1), (2,2), ... etc
diag[diag==0] = 1 # set according to your condition
a[range(N),range(N)] = diag #return the diagonal to its place
#tensor([[1, 9, 6, 6, 9, 9, 3, 1, 8, 4],
# [8, 1, 6, 8, 5, 8, 7, 8, 1, 4],
# [1, 9, 8, 4, 7, 0, 2, 9, 6, 2],
# [9, 5, 9, 6, 7, 1, 4, 0, 2, 6],
# [1, 2, 8, 0, 9, 0, 4, 3, 9, 9],
# [1, 4, 6, 9, 6, 5, 1, 2, 0, 7],
# [4, 8, 1, 3, 1, 6, 1, 3, 5, 6],
# [3, 8, 9, 9, 1, 3, 0, 9, 6, 6],
# [7, 4, 3, 0, 3, 5, 6, 6, 9, 2],
# [3, 1, 0, 8, 3, 5, 6, 6, 5, 5]])

How to add a list to a list of lists?

I'm coding a sudoku puzzle solver and I want to create a 9x9 grid where every cell is a list of it's own. The list sudokuGrid is the unsolved puzzle. It's a 2d list only. The list availableNumbers should be a 3d list where every empty cell (represented with a 0 in sudokuGrid) should have a list with the numbers 1-9.
How do I add the list?
sudokuGrid = []
sudokuGrid.append([0, 0, 8, 0, 0, 0, 0, 1, 0])
sudokuGrid.append([0, 9, 0, 0, 0, 0, 0, 0, 0])
sudokuGrid.append([3, 4, 0, 5, 9, 0, 0, 0, 7])
sudokuGrid.append([6, 8, 0, 0, 0, 0, 4, 0, 0])
sudokuGrid.append([0, 0, 0, 0, 7, 0, 0, 0, 0])
sudokuGrid.append([0, 0, 4, 8, 0, 0, 1, 0, 0])
sudokuGrid.append([0, 0, 6, 0, 8, 0, 0, 0, 5])
sudokuGrid.append([0, 5, 1, 0, 0, 0, 0, 2, 0])
sudokuGrid.append([0, 0, 0, 0, 2, 0, 0, 9, 0])
availableNumbers = []
for i in range (9):
for j in range(9):
if sudokuGrid[i][j] == 0:
availableNumbers[i][j][k] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
else:
availableNumbers[i][j][k] = sudokuGrid[i][j]
break
I get an error saying list index is out of range.
You need to initialize your availableNumbers list to have the same dimensions that you are trying to index to in order to add the values. You're getting an index error because availableNumbers[i][j][k] does not exist on an empty list. Also, you do not have k defined as anything. There's an easier way to do this without needing to initialize empty lists. Just use the copy module and make a copy of your sudokuGrid and replace all the 0 elements with a list of their potential values.
import copy
sudokuGrid = []
sudokuGrid.append([0, 0, 8, 0, 0, 0, 0, 1, 0])
sudokuGrid.append([0, 9, 0, 0, 0, 0, 0, 0, 0])
sudokuGrid.append([3, 4, 0, 5, 9, 0, 0, 0, 7])
sudokuGrid.append([6, 8, 0, 0, 0, 0, 4, 0, 0])
sudokuGrid.append([0, 0, 0, 0, 7, 0, 0, 0, 0])
sudokuGrid.append([0, 0, 4, 8, 0, 0, 1, 0, 0])
sudokuGrid.append([0, 0, 6, 0, 8, 0, 0, 0, 5])
sudokuGrid.append([0, 5, 1, 0, 0, 0, 0, 2, 0])
sudokuGrid.append([0, 0, 0, 0, 2, 0, 0, 9, 0])
availableNumbers = copy.deepcopy(sudokuGrid)
for i in range(0, len(availableNumbers)):
for x in range(0, len(availableNumbers[i])):
if availableNumbers[i][x] == 0:
availableNumbers[i][x] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(availableNumbers)
output:
[[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 8, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 1, [1, 2, 3, 4, 5, 6, 7, 8, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], 9, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9]],
[3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9], 5, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 7],
[6, 8, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 4, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 7, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 4, 8, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 1, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 6, [1, 2, 3, 4, 5, 6, 7, 8, 9], 8, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 5],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], 5, 1, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 2, [1, 2, 3, 4, 5, 6, 7, 8, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 2, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]]]
A few small changes will let you build up availablenumbers as you go:
sudokuGrid = []
sudokuGrid.append([0, 0, 8, 0, 0, 0, 0, 1, 0])
sudokuGrid.append([0, 9, 0, 0, 0, 0, 0, 0, 0])
sudokuGrid.append([3, 4, 0, 5, 9, 0, 0, 0, 7])
sudokuGrid.append([6, 8, 0, 0, 0, 0, 4, 0, 0])
sudokuGrid.append([0, 0, 0, 0, 7, 0, 0, 0, 0])
sudokuGrid.append([0, 0, 4, 8, 0, 0, 1, 0, 0])
sudokuGrid.append([0, 0, 6, 0, 8, 0, 0, 0, 5])
sudokuGrid.append([0, 5, 1, 0, 0, 0, 0, 2, 0])
sudokuGrid.append([0, 0, 0, 0, 2, 0, 0, 9, 0])
availableNumbers = []
for i in range (9):
availableNumbers.append([])
for j in range(9):
availableNumbers[i].append([])
if sudokuGrid[i][j] == 0:
availableNumbers[i][j] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
else:
availableNumbers[i][j] = sudokuGrid[i][j]
for a in availableNumbers:
print(a)
By appending to availableNumbers or availableNumbers[i] at the start of each loop we ensure that there is some space in availableNumbers where we can store our variables. That, along with removing your break statement and removing whatever k was will get you the following (line by line) output:
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 8, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 1, [1, 2, 3, 4, 5, 6, 7, 8, 9]]
[[1, 2, 3, 4, 5, 6, 7, 8, 9], 9, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9]]
[3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9], 5, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 7]
[6, 8, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 4, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9]]
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 7, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9]]
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 4, 8, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 1, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9]]
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 6, [1, 2, 3, 4, 5, 6, 7, 8, 9], 8, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 5]
[[1, 2, 3, 4, 5, 6, 7, 8, 9], 5, 1, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 2, [1, 2, 3, 4, 5, 6, 7, 8, 9]]
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 2, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]]

Fastest numpy way to remove a list of cells from a 2d array

I have a very large 2D numpy array of m x n elements. For each row, I need to remove exactly one element. So for example from a 4x6 matrix I might need to delete [0, 1], [1, 4], [2, 3], and [3, 3] - I have this set of coordinates stored in a list. In the end, the matrix will ultimately shrink in width by 1.
Is there a standard way to do this using a mask? Ideally, I need this to be as performant as possible.
Here is a method that use ravel_multi_index() to calculate one-dim index, and then delete() the elements, and reshape back to two-dim array:
import numpy as np
n = 12
a = np.repeat(np.arange(10)[None, :], n, axis=0)
index = np.random.randint(0, 10, n)
ravel_index = np.ravel_multi_index((np.arange(n), index), a.shape)
np.delete(a, ravel_index).reshape(n, -1)
the index:
array([4, 6, 9, 0, 3, 5, 3, 8, 9, 8, 4, 4])
the result:
array([[0, 1, 2, 3, 4, 5, 6, 7, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 9],
[0, 1, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8],
[0, 1, 2, 4, 5, 6, 7, 8, 9]])

how to convert an adjacency matrix into a list of links

How to convert python list into JSON format with just using the basic tools and utilities and include the specific "name", "group", "source", "target" key names etc? Is it basically alot of string concatenations to construct that format?
The list index represent connectivity, for example index 0 in the list connects to index 1, 3, 4, 5, 6, 7, 8, 9 and 1 connects to 0, 2, 3, 4, 5, 6, 7, 8, 9
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]]
Into something like this JSON format?
{"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}
]}
Just do:
import json
myjson = json.dumps(mylst)
Guessing from inputs and outputs, here is probably what you want.
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)
using the json module you should easily be able to encode the list.
As David suggested
json.dumps and
json.loads to turn from json to python. Python provides excellent documentation and typing in an unrefined search like 'python json' into google provides appropriate link as first search result

Categories

Resources