如何处理"解包的数量过多"

时间:2015-05-11 14:21:05

标签: python

我已经检查了一些想法和原因,下面针对这个问题进行了调查...... "Too many values to unpack" Exception (Stefano Borini的解释)

但是我在这里迭代列表作为理解列表并将结果移动到列表中......! 因此,输入的数量读取输出变量的数量,即tempList ...

那么,这个过程出了什么问题?!

def DoProcess(self, myList):
    tempList = []
    tempList = [[x,y,False] for [x,y] in myList]
    return tempList

编辑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

每个Point都有两个属性locationXlocationY ...

3 个答案:

答案 0 :(得分:5)

Agent的实施存在严重缺陷;你创建了一个无限生成器:

def __iter__(self):
    return self

def __next__(self):
    return [self.locationX, self.locationY]

这将永远产生具有两个值的列表。尝试在元组赋值中使用此对象将产生至少3个这样的值(2个用于xy目标,另外还有一个用于Python以了解要解压缩的值多于请求的值。每次需要序列中的另一个值时,Python所做的是调用__next__,并且每次代码只返回[x, y]。永远,永远,直到永恒。

__iter__方法应该返回两个值的实际迭代:

def __iter__(self):
    for value in (self.locationX, self.locationY):
        yield value

甚至只是

def __iter__(self):
    yield self.locationX
    yield self.locationY

完全放弃__next__。然后,上面的生成器将生成两个值,然后正确地引发StopIteration,并使用元组赋值。

__getitem__方法拼写全部小写并采用索引参数:

def __getitem__(self, index):
    return (self.locationX, self.locationY)[index]

现在0映射到locationX1映射到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]

答案 1 :(得分:0)

也许是这样的:

def DoProcess(self, myList):
    tempList = [[x[0],x[1],False] for x in myList]
    return tempList

答案 2 :(得分:0)

你可以这样做

def DoProcess(self, myList):
    return [sublist + [False] for sublist in myList]

为什么这不起作用有几种选择:

  1. myList的某个地方,您的子列表中包含的元素少于两个(或者更常见的是,子列表的长度不是2)。你可以用

    找到它们
    print [sublist for sublist in myList if len(sublist) != 2]
    
  2. myList中有些元素不是列表。你可以用

    找到它们
    print [element for element in myList if not isinstance(element, list)]
    
  3. 将它们与

    组合在一起
    print [element for element in myList if not isinstance(element, list) or len(element) != 2]