TAGS :Viewed: 7 - Published at: a few seconds ago

[ Appending a dictionary to another dictionary in a loop ]

I wrote this piece of code and I need some help to debug it. I would have to say that I have read some similar topic in this regard in this website but it could not help me to debug my code :)

OUTPUT:

Number of max devices per route: 4
Number of routes: 2
Level of mobility: 2
routes  at T 0 : {0: [28, 14, 7, 4], 1: [22, 0]}
routes  at T 1 : {0: [29, 20, 28], 1: [28]}
{0: {0: [29, 20, 28], 1: [28]}, 1: {0: [29, 20, 28], 1: [28]}}

My problem is that i want to have output like this:

{ 0:{0: [28, 14, 7, 4], 1: [22, 0]} , 1: {0: [29, 20, 28], 1: [28]},}

but, I do not know why the last dictionary is repeated again in new dictionary. I tried to debug it but i was unable to be successful. So, how can I append a dictionary to another dictionary in a loop ?

class myClassName(object):
    def __init__(self):
        """
        Class constructor.
        """

        self.no_devices = input("Number of max devices per route: ")
        self.no_routes = input("Number of routes: ")
        self.mob=input("Level of mobility: ")
        self.all_routes={}
        self.routes = {}

        self.arr = [0,1,2,3,4,5,6,7,8,9,10...,29] 

        for j in xrange(self.mob):
            for i in range(self.no_routes):
                random.shuffle(self.arr)                                          
                self.routes[i] = list(self.arr[0: random.randint(1,self.no_devices)])          
                self.all_routes[j]=self.routes


            print "routes  at T" ,j, ":" ,self.routes


        print self.all_routes

Answer 1


You could do something like: orig_dict.update(new_dict)

Note: If both dictionaries have some similar keys,the values will be picked from the new_dict

Edit:

I think the problem you might be having is because you are overwriting dict with list(routes & all_routes)

Answer 2


As @shaktimaan indicated self.routes in each iteration of the loop is overwritten, and self.all_routes keeps only a reference to self.routes. I modified the code, once to make it work with python 3.4 (i.e. version of python I have, sorry, I don't work with 2.x now) and also to fix the overwriting problem.

import random

class myClassName(object):
    def __init__(self):
        """
        Class constructor.
        """

        self.no_devices = int(input("Number of max devices per route: "))
        self.no_routes = int(input("Number of routes: "))
        self.mob = int(input("Level of mobility: "))
        self.all_routes={}


        self.arr = list(range(0,30))

        for j in range(self.mob):
            routes = {}
            for i in range(self.no_routes):
                random.shuffle(self.arr)                                          
                routes[i] = list(self.arr[0: random.randint(1,self.no_devices)])          
                self.all_routes[j]=routes


            print("routes  at T" ,j, ":" ,routes)


        print(self.all_routes)



if __name__ == '__main__':
    myClassName()

Example output:

Number of max devices per route: 4
Number of routes: 2
2Level of mobility: 
routes  at T 0 : {0: [0, 10], 1: [22, 14]}
routes  at T 1 : {0: [16], 1: [22, 3, 5, 17]}
{0: {0: [0, 10], 1: [22, 14]}, 1: {0: [16], 1: [22, 3, 5, 17]}}

Hope this will be useful to you.