python - how to handle "Too many values to unpack" -


i have checked idea , reason, investigated below problem... "too many values unpack" exception (stefano borini's explanation)

but here iterating through list comprehension list , move result list...! number of inputs reads number of output variable, i.e. templist...

then, wrong process?!

def doprocess(self, mylist):     templist = []     templist = [[x,y,false] [x,y] in mylist]     return templist 

edit 1: mylist list of lists, [[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] x in mylist]         return mylist 

and each point has 2 attribute locationx , locationy...

your implementation of agent severely flawed; created infinite generator:

def __iter__(self):     return self  def __next__(self):     return [self.locationx, self.locationy] 

this forever yield lists 2 values. trying use object in tuple assignment yield @ least 3 such values (2 x , y targets, plus 1 more python know there more values unpack requested). python call __next__ each time needs value in sequence, , code returns [x, y] each time. ever , ever until eternity.

the __iter__ method should return actual iteration on 2 values instead:

def __iter__(self):     value in (self.locationx, self.locationy):         yield value 

or just

def __iter__(self):     yield self.locationx     yield self.locationy 

dropping __next__ altogether. above generator yield 2 values raise stopiteration properly, , work tuple assignment.

the __getitem__ method spelled all lowercase , takes index argument:

def __getitem__(self, index):     return (self.locationx, self.locationy)[index] 

now 0 maps locationx , 1 locationy.

rewriting code changes:

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 = []         _ 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] x, y in mylist] 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -