2013-11-20 15 views
0

我正在開發一個使用pygame的N-Queen模擬。如何將python列表動態地傳遞給另一個python文件

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程序動態地傳遞列表,以便可以看到確切的回溯程序。 謝謝

+0

將兩個程序同時運行?如果沒有,你可以[列出](http://docs.python.org/2/library/pickle.html)列表並將它保存在磁盤上,pygame過程可以取消數據並執行任何操作。 –

+1

另一個Python文件?你的意思是[模塊](http://docs.python.org/2/tutorial/modules.html)?也許你會簡單地導入模塊並調用函數?請澄清我是否誤解了你。 –

回答

1

如果您想知道遞歸函數內部發生了什麼,您可以添加外部函數作爲參數,並且您可以在遞歸中使用它來打印algorythm的當前狀態或在棋盤上繪製皇后。

在示例中,我使用show_colums()打印self.columns每次self.place()正在運行。

文件: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結果

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] 
+0

這正是我想要的。謝謝 – dvs

+0

你只是想調試你的遞歸函數?然後我建議使用調試器,而不是進行「打印」調試。現在可能已經足夠了,但請習慣於調試。如果pdb對你的口味來說太神祕了,試試Winpdb。它有一個很好的GUI和許多不錯的功能。 –

+0

不,我想把這些不斷變化的列表傳遞給另一個函數,以模擬一些運動 – dvs

1

如果我正確地理解了你,你只是想傳遞一個基本的python類型列表到你想從這裏調用的另一個Python進程。你可以使用任何序列化和反序列化的方式。

容易的將是(如果您的列表實際上只是包含了基本的類型,如整數,浮點,字符串,...)使用JSON

import json 
import subprocess 

list_to_transfer = [1,2,"sdf",5.6] 
list_as_string = json.dumps(list_to_transfer) 

subprocess.call("other_executable \"%s\"" % list_as_string, shell=True) 

在其他進程中,僅僅通過杜安

反序列化
import json 
import sys 

list_from_other_process = json.loads(sys.argv[1]) 

我沒有試過那個代碼,也許它沒有運行,但主要想法應該清楚。

1

如果在同一目錄下有兩個文件,你可以這樣做:

  • file1.py

    def function1(list): 
        print(list) 
    
  • file2.py

    import file1 
    file1.function1(['hallo']) 
    

你可以使用import是。

相關問題