我已经检查了一些想法和原因,下面针对这个问题进行了调查...... "太多值解包"异常 (Stefano Borini的解释)
但是在这里,我正在迭代列表作为理解列表并将结果移动到列表中......!所以输入的数量读取输出变量的数量,即tempList......
然后,这个过程出了什么问题?!
def DoProcess(self, myList):
tempList = []
tempList = [[x,y,False] for [x,y] in myList]
return tempList
Run Code Online (Sandbox Code Playgroud)
编辑1:myList是列表,就像[[x1, y1], [x2, y2], [x3, y3], [x4 y4]].
class Agent(object):
def __init__(self, point = None):
self.locationX = point.x
self.locationY = point.y
def __iter__(self):
return self
def __next__(self):
return [self.locationX, self.locationY]
def __getItem__(self):
return [self.locationX, self.locationY]
def GenerateAgents(self, numberOfAgents):
agentList = []
while len(agentList) < numberOfAgents:
point = Point.Point()
point.x = random.randint(0, 99)
point.y = random.randint(0, 99)
agent = Agent(point)
agentList.append(agent)
return agentList
def DoProcess(self, myList):
tempList = []
tempList = [[x[0],x[1],False] for x in myList]
return myList
Run Code Online (Sandbox Code Playgroud)
每个Point都有两个属性locationX和locationY...
你的实施存在Agent严重缺陷; 你创造了一个无限的发电机
def __iter__(self):
return self
def __next__(self):
return [self.locationX, self.locationY]
Run Code Online (Sandbox Code Playgroud)
这将永远产生具有两个值的列表.尝试在元组赋值中使用此对象将产生至少3个这样的值(对于x和y目标而言为2 ,再加上Python的一个,以便知道有更多值要解压缩而不是请求).Python __next__每次在序列中需要另一个值时都会调用它,并且[x, y]每次都会返回代码.永远永远.
该__iter__方法应该返回两个值的实际迭代:
def __iter__(self):
for value in (self.locationX, self.locationY):
yield value
Run Code Online (Sandbox Code Playgroud)
甚至只是
def __iter__(self):
yield self.locationX
yield self.locationY
Run Code Online (Sandbox Code Playgroud)
放弃__next__了.然后,上面的生成器将生成两个值,然后StopIteration正确引发,并使用元组赋值.
该__getitem__方法拼写全部小写并采用索引参数:
def __getitem__(self, index):
return (self.locationX, self.locationY)[index]
Run Code Online (Sandbox Code Playgroud)
现在,0映射到locationX和1到locationY.
使用这些更改重写代码:
class Agent(object):
def __init__(self, point):
self.locationX = point.x
self.locationY = point.y
def __iter__(self):
yield self.locationX
yield self.locationY
def __getitem__(self, index):
return (self.locationX, self.locationY)[index]
def GenerateAgents(self, numberOfAgents):
agentList = []
for _ in range(numberOfAgents):
point = Point.Point()
point.x = random.randint(0, 99)
point.y = random.randint(0, 99)
agent = Agent(point)
agentList.append(agent)
return agentList
def DoProcess(self, myList):
return [[x, y, False] for x, y in myList]
Run Code Online (Sandbox Code Playgroud)