Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80227 > unrolled thread
| Started by | Anurag Patibandla <anuragpatibandla7@gmail.com> |
|---|---|
| First post | 2014-11-04 11:37 -0800 |
| Last post | 2014-11-04 12:56 -0800 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
Problem adding a Key Value pair Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-11-04 11:37 -0800
Re: Problem adding a Key Value pair MRAB <python@mrabarnett.plus.com> - 2014-11-04 20:19 +0000
Re: Problem adding a Key Value pair Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-11-04 12:56 -0800
| From | Anurag Patibandla <anuragpatibandla7@gmail.com> |
|---|---|
| Date | 2014-11-04 11:37 -0800 |
| Subject | Problem adding a Key Value pair |
| Message-ID | <32931127-fc4d-4ea5-87eb-f763a4fdf52c@googlegroups.com> |
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']
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2014-11-04 20:19 +0000 |
| Message-ID | <mailman.15472.1415132384.18130.python-list@python.org> |
| In reply to | #80227 |
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]
[toc] | [prev] | [next] | [standalone]
| From | Anurag Patibandla <anuragpatibandla7@gmail.com> |
|---|---|
| Date | 2014-11-04 12:56 -0800 |
| Message-ID | <1559df04-46f6-4cb9-a31a-4ca5fb22ac90@googlegroups.com> |
| In reply to | #80227 |
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!!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web