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.
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, 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, 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]]
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