如何将python列表动态传递给另一个python文件 [英] How to pass python list dynamically to another python file
问题描述
我正在使用pygame开发N级皇后模拟游戏.
I am developing an N-Queen Simulation using pygame.
class NQ:
def __init__(self,n):
self.size = n
self.columns = [] * self.size
self.places = 0
self.backtracks = 0
def place(self, startRow=0):
if len(self.columns) == self.size:
return self.columns
else:
for row in range(startRow, self.size):
if self.isSafe(len(self.columns), row) is True:
self.columns.append(row)
self.places += 1
return self.place()
else:
lastRow = self.columns.pop()
self.backtracks += 1
return self.place(startRow=lastRow + 1)
def isSafe(self, col, row):
for threatRow in self.columns:
threatCol = self.columns.index(threatRow)
if row == threatRow or col == self.columns.index(threatRow):
return False
elif threatRow + threatCol == row + col or threatRow - threatCol == row - col:
return False
return True
def process(n):
nqueens = NQ(n)
nqueens.place(0)
return nqueens.columns
我还在另一个文件中有一个pygame程序来画棋盘,该棋盘将一个列表作为输入并相应地放置它们.
如果我想显示皇后的动作,我如何从递归code
过程动态传递列表,以便可以看到确切的回溯过程.
谢谢
Also I have a pygame procedure in another file to draw chess board which takes a list as input and place them accordingly.
If I want to show the movement of queens, how can i pass the list dynamically from the recursivecode
procedure so that the exact backtracking procedure is visible.
Thank you
推荐答案
如果您想知道递归函数内部发生了什么,可以添加外部函数作为参数,然后可以在递归内部使用它来打印算法的当前状态.或在棋盘上画皇后.
If you want to know what is going on inside recursive function you can add external function as argument and than you can use it inside recursion to print current state of algorythm or draw queens on chess board.
在示例中,每次self.place()
运行时,我都使用show_colums()
打印self.columns
.
In example I use show_colums()
to print self.columns
every time self.place()
is running.
文件:nq.py
class NQ:
def __init__(self,n, callback): # added callback
self.size = n
self.columns = []
self.places = 0
self.backtracks = 0
self.callback = callback # added callback
def place(self, startRow=0):
self.callback(self.columns) # added callback
if len(self.columns) == self.size:
return self.columns
else:
for row in range(startRow, self.size):
if self.isSafe(len(self.columns), row) is True:
self.columns.append(row)
self.places += 1
return self.place()
else:
lastRow = self.columns.pop()
self.backtracks += 1
return self.place(startRow=lastRow + 1)
def isSafe(self, col, row):
for threatRow in self.columns:
threatCol = self.columns.index(threatRow)
if row == threatRow or col == self.columns.index(threatRow):
return False
elif threatRow + threatCol == row + col or threatRow - threatCol == row - col:
return False
return True
文件:main.py
file: main.py
from nq inport NQ
def show_columns(x):
print "columns:", x
def process(n):
nqueens = NQ(n, show_columns)
nqueens.place(0)
return nqueens.columns
process(8)
部分结果
columns: []
columns: [0]
columns: [0, 2]
columns: [0, 2, 4]
columns: [0, 2, 4, 1]
columns: [0, 2, 4, 1, 3]
columns: [0, 2, 4, 1]
columns: [0, 2, 4, 1, 7]
columns: [0, 2, 4, 1]
这篇关于如何将python列表动态传递给另一个python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!