如何访问不同功能的列表 [英] how to access the list in different function
问题描述
- def maxvalue
- def min值
- def getAction
在def maxvalue函数中,我列出了一系列操作。我想要在def getaction函数中访问该列表,以便我可以反转列表,然后从中取出第一个条目。我怎么能?
def getAction(self,gamestate):
bestaction.reverse()
return bestaction [0]
$ b def maxvalue(gameState,depth):
actions = gameState.getLegalActions(0 );
v = -9999
bestaction = []
在动作中的动作:
marks = minvalue(gameState.generateSuccessor(0,action),depth,1)
if (v <标记):
v =标记
bestaction.append(动作)
返回v
这给了我一个错误.....全球名称bestaction未定义
<发布实际代码是一个好主意 - 它可以更容易地看到发生了什么。
有了这个说法,你可能想要这样的东西:
class MyClass(object):
def max_value(self):
#像这样分配你的列表将使它可访问
#从类中的其他方法
self.list_in_max_value = [ A,B,C]
def get_action(self):
#在这里,我们正在做一些与
self.list_in_max_value.reverse ()
return self.list_in_max_value [0]
>>> my_class = MyClass()
>>> my_class.max_value()
>>> my_class.get_action()
C
您可能需要阅读 python类教程。
I have made a class in which there are 3 functions.
- def maxvalue
- def min value
- def getAction
In the def maxvalue function, I have made a list of actions. I want that list to be accessed in def getaction function so that I can reverse the list and then take out the first entry from it. How can do i that??
def getAction(self,gamestate):
bestaction.reverse()
return bestaction[0]
def maxvalue(gameState, depth):
actions = gameState.getLegalActions(0);
v = -9999
bestaction = []
for action in actions:
marks = minvalue(gameState.generateSuccessor(0, action), depth, 1)
if(v < marks):
v = marks
bestaction.append(action)
return v
It is giving me an error....."global name bestaction is not defined"
It's a good idea to post actual code - it makes it easier to see what's happening.
With that said, you probably want something like this:
class MyClass(object):
def max_value(self):
# assigning your list like this will make it accessible
# from other methods in the class
self.list_in_max_value = ["A", "B", "C"]
def get_action(self):
# here, we're doing something with the list
self.list_in_max_value.reverse()
return self.list_in_max_value[0]
>>> my_class = MyClass()
>>> my_class.max_value()
>>> my_class.get_action()
"C"
You might want to read through the python class tutorial.
这篇关于如何访问不同功能的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!