只有二维数组中的第一个数组被函数更新
Only the first array inside a 2D array being updated by function
我在下面编写了这段代码来创建一个随机的数独拼图网格:
import time
import random
import math
sudoLine = [0,0,0,0,0,0,0,0,0]
#Creating mainGrid, this is a blank sudoku grid which I will place the contents of the possibleNums array into in a random order for each line.
mainGrid = [
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
]
possibleNums = [1,2,3,4,5,6,7,8,9]
i = 0
#Grabbing the length of the possibleNums array and reducing it by 1 to get the index position of the last element.
length = len(possibleNums) - 1
def LineFill(line,nums,count):
length = len(nums) - 1
i = 0
while length > 0:
length = len(nums) - 1
r = random.randint(0,length)
line[i] = nums[r]
nums.pop(r)
i += 1
return line
n=0
这个 while 循环然后在那个下面,循环 mainGrid
,每次改变正在填充的数组:
while n in range(0,len(mainGrid)):
mainGrid[n] = LineFill(mainGrid[n],possibleNums,i)
n += 1
print(mainGrid)
当我 运行 它时,只有数独网格的第一行被填充,这意味着只有列表中的第一个数组被更新,即使 while 循环应该循环遍历它们。
有人能指出我逻辑上的错误吗?
谢谢。
您的线路 nums.pop()
是问题所在。您将 possibleNums
作为 LineFill
参数 nums
传递。所以 nums
只是对 possibleNums
的引用。填充一行后,possibleNums
是一个空列表。
如果您的目的是随机填充一个数字网格,在任何给定行上都没有重复项,这里有一个更简单的方法:
In [1]: import random
In [2]: [random.sample(range(1, 10), 9) for _ in range(9)]
Out[2]:
[[3, 9, 8, 1, 2, 5, 6, 4, 7],
[6, 7, 3, 9, 2, 5, 8, 1, 4],
[4, 1, 9, 2, 7, 3, 5, 6, 8],
[3, 5, 9, 4, 2, 1, 7, 8, 6],
[7, 2, 8, 6, 1, 9, 4, 5, 3],
[1, 8, 5, 9, 4, 6, 2, 7, 3],
[8, 7, 1, 9, 2, 5, 3, 4, 6],
[7, 6, 1, 5, 4, 2, 9, 3, 8],
[7, 8, 4, 2, 5, 1, 6, 3, 9]]
请注意,此方法不会产生有效的数独解法(理论上是可能的,但概率……对您不利)——但您似乎并不希望这样做无论如何。
我在下面编写了这段代码来创建一个随机的数独拼图网格:
import time
import random
import math
sudoLine = [0,0,0,0,0,0,0,0,0]
#Creating mainGrid, this is a blank sudoku grid which I will place the contents of the possibleNums array into in a random order for each line.
mainGrid = [
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
]
possibleNums = [1,2,3,4,5,6,7,8,9]
i = 0
#Grabbing the length of the possibleNums array and reducing it by 1 to get the index position of the last element.
length = len(possibleNums) - 1
def LineFill(line,nums,count):
length = len(nums) - 1
i = 0
while length > 0:
length = len(nums) - 1
r = random.randint(0,length)
line[i] = nums[r]
nums.pop(r)
i += 1
return line
n=0
这个 while 循环然后在那个下面,循环 mainGrid
,每次改变正在填充的数组:
while n in range(0,len(mainGrid)):
mainGrid[n] = LineFill(mainGrid[n],possibleNums,i)
n += 1
print(mainGrid)
当我 运行 它时,只有数独网格的第一行被填充,这意味着只有列表中的第一个数组被更新,即使 while 循环应该循环遍历它们。
有人能指出我逻辑上的错误吗?
谢谢。
您的线路 nums.pop()
是问题所在。您将 possibleNums
作为 LineFill
参数 nums
传递。所以 nums
只是对 possibleNums
的引用。填充一行后,possibleNums
是一个空列表。
如果您的目的是随机填充一个数字网格,在任何给定行上都没有重复项,这里有一个更简单的方法:
In [1]: import random
In [2]: [random.sample(range(1, 10), 9) for _ in range(9)]
Out[2]:
[[3, 9, 8, 1, 2, 5, 6, 4, 7],
[6, 7, 3, 9, 2, 5, 8, 1, 4],
[4, 1, 9, 2, 7, 3, 5, 6, 8],
[3, 5, 9, 4, 2, 1, 7, 8, 6],
[7, 2, 8, 6, 1, 9, 4, 5, 3],
[1, 8, 5, 9, 4, 6, 2, 7, 3],
[8, 7, 1, 9, 2, 5, 3, 4, 6],
[7, 6, 1, 5, 4, 2, 9, 3, 8],
[7, 8, 4, 2, 5, 1, 6, 3, 9]]
请注意,此方法不会产生有效的数独解法(理论上是可能的,但概率……对您不利)——但您似乎并不希望这样做无论如何。