0
我已經給了一個實現優先級隊列的類,使用函數來評估優先級。如何將一個變量的兩個參數傳遞給一個函數?
class PriorityQueueWithFunction(PriorityQueue):
"""
Implements a priority queue with the same push/pop signature of the
Queue and the Stack classes. This is designed for drop-in replacement for
those two classes. The caller has to provide a priority function, which
extracts each item's priority.
"""
def __init__(self, priorityFunction):
# type: (object) -> object
"priorityFunction (item) -> priority"
self.priorityFunction = priorityFunction # store the priority function
PriorityQueue.__init__(self) # super-class initializer
def push(self, item):
"Adds an item to the queue with priority from the priority function"
PriorityQueue.push(self, item, self.priorityFunction(item))
我也被賦予了優先級函數,我將初始化上面的類。
def manhattanHeuristic(position, problem, info={}):
"The Manhattan distance heuristic for a PositionSearchProblem"
xy1 = position
xy2 = problem.goal
return abs(xy1[0] - xy2[0]) + abs(xy1[1] - xy2[1])
上面的代碼給我們,我們不能改變它。我必須創建該PriorityQueueWithFunction類並將一個元素推送給它。 推我的課程的功能上的參數,項目。但是我的PriorityFunction需要2個。 我應該用什麼樣的論據來將正確的elemnt推入我的課堂,並使我的優先功能正常工作?
這就是我想和我收到編譯錯誤,manhattanHeuristic ...需要兩個參數,1中給出
#Creating a queingFn
queuingFn = PriorityQueueWithFunction(heuristic)
Frontier = queuingFn
#Creating the item that needs to be pushed
StartState = problem.getStartState()
StartNode = (StartState,'',0,(-1,-1))
#Here is my problem
item = StartState , problem
Frontier.push(item)
我應該改變我的項目的形式?有任何想法嗎 ?
看看'* args'和'** kwargs' 。它們允許您使用單個(或兩個)變量在函數中傳遞任意數量的數據。您可以將這些內容解壓縮到函數中。 –
在你的情況下,它應該可以像添加一個星號'*'在'item'的infront一樣簡單。 –