Python 'Dictionary changed size during iteration' error even though size changes after iterations should be complete -


i have list of cities , trying group them based on distance. iterating through list of cities. first city in list has nothing compare to, automatically creates own city group. subsequent cities in list compared of cities in existing groups of cities. if city within threshold distance of of cities in city group, added city group. otherwise, creates new group. groups being stored dictionaries 1 dictionary storing city groups , each city group there dictionary storing of cities in group.

i calling findsetlist() checks see if city should belong existing city group or create new one. after calling of findsetlist(), update city group dictionary either addition of city existing group or creation of new group. when run code, getting error says "dictionary changed size during iteration". saying changing size of dictionary while iterating inside of 'for loops'. don't change size until after should have exited both 'for loops', don't understand why having problem.

i appreciate can give me. thank you.

class citysets:       def __init__(self,city,citysetdict,citysetnum):           self.city=city           self.citysetdict=citysetdict           self.citysetnum=citysetnum       def findsetlists(self):           if len (self.citysetdict)==0:               self.citysetnum=1               self.closecitysetnum=1               self.closeval=9999999999               self.temp={}               self.temp[self.city[apiad]]=self.city           else:               self.closecitysetnum=self.citysetnum+1               self.closeval=9999999999               self.temp={}               self.key in self.citysetdict:                   self.key2 in self.citysetdict[self.key]:                       self.secondcity= self.citysetdict[self.key][self.key2]                       self.compare=citycompare(self.city,self.secondcity)                       self.compare.distance()                       if self.compare.dist<=distancethreshold:                           self.closecitysetnum=self.key                           self.temp = self.citysetdict[self.key]                           self.temp[self.city[apiad]]=self.city    citysets={}   currentcity in citylist:       d=citysets(currentcity,citysets,citysetnum)       d.findsetlists()       citysetnum=d.closecitysetnum       citysets[citysetnum]=d.temp   

            ...             self.key2 in self.citysetdict[self.key]:                  ....                  if self.compare.dist<=distancethreshold:                      ...                     self.temp = self.citysetdict[self.key]                     self.temp[self.city[apiad]]=self.city 

this looks problem. you're iterating on dictionary self.citysetdict[self.key]. assign self.citysetdict[self.key] self.temp, , assign item of self.temp (which change size unless key existed).

as aside, when python reported error "dictionary size changed during iteration", it have told line problem. clue have saved confusion how thought weren't changing size until after loops finished, because have pointed line within loops (which narrows down dictionary problem). if don't yet know how read stack traces printed in error message kind of problem identification (which fine, has learn sometime), when you're asking others please include error message, people helping can make use of clue!


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -