Re: Problem adding a Key Value pair
On Tuesday, November 4, 2014 2:37:49 PM UTC-5, Anurag Patibandla wrote: > I am trying to add a key value pair of ("Priority":"1") to queue1, > ("Priority":"2") to queue2, and ("Priority":"3") to queue3. > When I just add ("Priority":"1") to queue1, it works. > But when I run the above code, ("Priority":"3") is being added to all the > queues. > This looks trivial and I don't understand why this is happening. Is there > something wrong with what I am doing? > > json_split = {} > value = {"Status": "Submitted", "m_Controller": "Python"} > a = range(31) > del a[0] > for i in a: > json_split[i] = value > keys = json_split.keys() > order = list(keys) > q1 = int(round(len(keys)*0.2)) > q2 = int(round(len(keys)*0.3)) > q3 = int(round(len(keys)*0.5)) > b = [q1,q2,q3] > n=0 > threedicts = [] > for i in b: > queues = order[n:n+i] > n = n+i > lists = [(queues[j], json_split.get(queues[j])) for j in > range(len(queues))] > onedict = {} > for q in queues: > onedict[q] = json_split[q] > threedicts.append (onedict) > queue1, queue2, queue3 = threedicts > keys1 = queue1.keys() > for i in keys1: > queue1[i]['Priority'] = ['1'] > keys2 = queue2.keys() > for j in keys2: > queue2[j]['Priority'] = ['2'] > keys3 = queue3.keys() > for z in keys3: > queue3[z]['Priority'] = ['3'] Awesome! Thank you!! -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem adding a Key Value pair
On 2014-11-04 19:37, Anurag Patibandla wrote: I am trying to add a key value pair of ("Priority":"1") to queue1, ("Priority":"2") to queue2, and ("Priority":"3") to queue3. When I just add ("Priority":"1") to queue1, it works. But when I run the above code, ("Priority":"3") is being added to all the queues. This looks trivial and I don't understand why this is happening. Is there something wrong with what I am doing? json_split = {} value = {"Status": "Submitted", "m_Controller": "Python"} a = range(31) del a[0] That's better as: a = range(1, 31) for i in a: json_split[i] = value Here the key will be whatever 'i' refers to (1..30) and the value will be the dict referred to by 'value'. Try adding the line: print json_split[1] is json_split[2] It'll print out 'True'; it's saying that they are the same dict. You want the values of json_split to be _separate_ dicts. The fix is simple. Just make a copy of the dict for each one: for i in a: json_split[i] = dict(value) [snip] -- https://mail.python.org/mailman/listinfo/python-list
Problem adding a Key Value pair
I am trying to add a key value pair of ("Priority":"1") to queue1, ("Priority":"2") to queue2, and ("Priority":"3") to queue3. When I just add ("Priority":"1") to queue1, it works. But when I run the above code, ("Priority":"3") is being added to all the queues. This looks trivial and I don't understand why this is happening. Is there something wrong with what I am doing? json_split = {} value = {"Status": "Submitted", "m_Controller": "Python"} a = range(31) del a[0] for i in a: json_split[i] = value keys = json_split.keys() order = list(keys) q1 = int(round(len(keys)*0.2)) q2 = int(round(len(keys)*0.3)) q3 = int(round(len(keys)*0.5)) b = [q1,q2,q3] n=0 threedicts = [] for i in b: queues = order[n:n+i] n = n+i lists = [(queues[j], json_split.get(queues[j])) for j in range(len(queues))] onedict = {} for q in queues: onedict[q] = json_split[q] threedicts.append (onedict) queue1, queue2, queue3 = threedicts keys1 = queue1.keys() for i in keys1: queue1[i]['Priority'] = ['1'] keys2 = queue2.keys() for j in keys2: queue2[j]['Priority'] = ['2'] keys3 = queue3.keys() for z in keys3: queue3[z]['Priority'] = ['3'] -- https://mail.python.org/mailman/listinfo/python-list