Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79107 > unrolled thread
| Started by | anuragpatibandla7@gmail.com |
|---|---|
| First post | 2014-10-14 08:57 -0700 |
| Last post | 2014-10-15 18:25 -0400 |
| Articles | 20 on this page of 28 — 6 participants |
Back to article view | Back to comp.lang.python
Parsing Python dictionary with multiple objects anuragpatibandla7@gmail.com - 2014-10-14 08:57 -0700
Re:Parsing Python dictionary with multiple objects Dave Angel <davea@davea.name> - 2014-10-14 13:02 -0400
Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-14 14:15 -0700
Re: Parsing Python dictionary with multiple objects Skip Montanaro <skip.montanaro@gmail.com> - 2014-10-14 16:32 -0500
Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-14 14:36 -0700
Re: Parsing Python dictionary with multiple objects MRAB <python@mrabarnett.plus.com> - 2014-10-14 23:58 +0100
Re:Parsing Python dictionary with multiple objects Dave Angel <davea@davea.name> - 2014-10-14 21:57 -0400
Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-14 19:32 -0700
Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-14 20:40 -0700
Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-14 21:50 -0700
Re: Parsing Python dictionary with multiple objects Dave Angel <davea@davea.name> - 2014-10-15 13:43 -0400
Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 11:35 -0700
Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-14 20:41 -0700
Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 08:52 -0700
Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-15 09:06 -0700
Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 09:28 -0700
Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-15 09:43 -0700
Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-15 09:59 -0700
Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 09:57 -0700
Re: Parsing Python dictionary with multiple objects Dave Angel <davea@davea.name> - 2014-10-15 13:48 -0400
Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 10:00 -0700
Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-15 10:10 -0700
Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 10:20 -0700
Re: Parsing Python dictionary with multiple objects Dave Angel <davea@davea.name> - 2014-10-15 13:50 -0400
Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 10:20 -0700
Re: Parsing Python dictionary with multiple objects Rustom Mody <rustompmody@gmail.com> - 2014-10-15 10:35 -0700
Re: Parsing Python dictionary with multiple objects Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-10-15 11:27 -0700
Re: Parsing Python dictionary with multiple objects Dave Angel <davea@davea.name> - 2014-10-15 18:25 -0400
Page 1 of 2 [1] 2 Next page →
| From | anuragpatibandla7@gmail.com |
|---|---|
| Date | 2014-10-14 08:57 -0700 |
| Subject | Parsing Python dictionary with multiple objects |
| Message-ID | <64f7e57a-4be3-44f4-b11c-0d270fc91809@googlegroups.com> |
I have a dictionary that looks like this:
{"1":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
"2":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
"3":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
"4":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"}}
Now if I have 100 objects like these and I need to split them into 3 smaller dicts in a ratio 2:3:5, how could I do that?
I tried using numpy.random.choice(), but it says it needs to be a 1-d array.
Can someone please help with this?
Thanks
[toc] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-10-14 13:02 -0400 |
| Message-ID | <mailman.14856.1413305953.18130.python-list@python.org> |
| In reply to | #79107 |
anuragpatibandla7@gmail.com Wrote in message:
> I have a dictionary that looks like this:
> {"1":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
> "2":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
> "3":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
> "4":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"}}
>
> Now if I have 100 objects like these and I need to split them into 3 smaller dicts in a ratio 2:3:5, how could I do that?
I really have no idea what that means. You have 100 dicts of
dicts? Are the keys unique? If so, you could combine them with a
loop of update.
> I tried using numpy.random.choice(), but it says it needs to be a 1-d array.
>
How about random.choice? It needs a sequence.
To get anything more concrete, you need to specify Python version,
and make a clearer problem statement, perhaps with example of
what you expect.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Anurag Patibandla <anuragpatibandla7@gmail.com> |
|---|---|
| Date | 2014-10-14 14:15 -0700 |
| Message-ID | <b3d199b6-c9ed-4efb-acf2-93f16b5dd8d3@googlegroups.com> |
| In reply to | #79108 |
On Tuesday, October 14, 2014 12:59:27 PM UTC-4, Dave Angel wrote:
> anuragpatibandla7@gmail.com Wrote in message:
>
> > I have a dictionary that looks like this:
>
> > {"1":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
>
> > "2":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
>
> > "3":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
>
> > "4":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"}}
>
> >
>
> > Now if I have 100 objects like these and I need to split them into 3 smaller dicts in a ratio 2:3:5, how could I do that?
>
>
>
> I really have no idea what that means. You have 100 dicts of
>
> dicts? Are the keys unique? If so, you could combine them with a
>
> loop of update.
>
>
>
> > I tried using numpy.random.choice(), but it says it needs to be a 1-d array.
>
> >
>
>
>
> How about random.choice? It needs a sequence.
>
>
>
> To get anything more concrete, you need to specify Python version,
>
> and make a clearer problem statement, perhaps with example of
>
> what you expect.
>
>
>
>
>
> --
>
> DaveA
Hey DaveA,
I am using Python 2.7.
Yes. I have a dict of dicts with keys ranging from 1..100
And the value of each of these keys is another dict. What I need to do is make 3 smaller dicts and assign the values of keys 1..100 in the ratio 2:3:5.
For example, if my original dict is
d={"1":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
"2":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
"3":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
"4":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"}}
...
...
"100":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"}
I need to have three dicts d1, d2, d3 with d1 containing the values of first 20 keys, d2 containing the values on next 30 keys and d3 containing the values of the next 50.
Can you please help me with this?
[toc] | [prev] | [next] | [standalone]
| From | Skip Montanaro <skip.montanaro@gmail.com> |
|---|---|
| Date | 2014-10-14 16:32 -0500 |
| Message-ID | <mailman.14864.1413322368.18130.python-list@python.org> |
| In reply to | #79117 |
[Multipart message — attachments visible in raw view] — view raw
Shuffle the keys, then grab the first 20 for one dictionary, the next 30 for the second, and the last 50 for the third. Skip
[toc] | [prev] | [next] | [standalone]
| From | Anurag Patibandla <anuragpatibandla7@gmail.com> |
|---|---|
| Date | 2014-10-14 14:36 -0700 |
| Message-ID | <9eaa23e4-a38a-4318-a899-4c7369bf6e50@googlegroups.com> |
| In reply to | #79118 |
On Tuesday, October 14, 2014 5:33:01 PM UTC-4, Skip Montanaro wrote: > Shuffle the keys, then grab the first 20 for one dictionary, the next 30 for the second, and the last 50 for the third. > > Skip Could you please be more specific?
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2014-10-14 23:58 +0100 |
| Message-ID | <mailman.14866.1413327514.18130.python-list@python.org> |
| In reply to | #79117 |
On 2014-10-14 22:15, Anurag Patibandla wrote:
> On Tuesday, October 14, 2014 12:59:27 PM UTC-4, Dave Angel wrote:
>> anuragpatibandla7@gmail.com Wrote in message:
>>
>> > I have a dictionary that looks like this:
>> > {"1":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
>> > "2":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
>> > "3":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
>> > "4":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"}}
>> >
>> > Now if I have 100 objects like these and I need to split them into 3 smaller dicts in a ratio 2:3:5, how could I do that?
>>
>> I really have no idea what that means. You have 100 dicts of
>> dicts? Are the keys unique? If so, you could combine them with a
>> loop of update.
>>
>> > I tried using numpy.random.choice(), but it says it needs to be a 1-d array.
>> >
>>
>> How about random.choice? It needs a sequence.
>>
>> To get anything more concrete, you need to specify Python version,
>> and make a clearer problem statement, perhaps with example of
>> what you expect.
>>
>
> Hey DaveA,
> I am using Python 2.7.
> Yes. I have a dict of dicts with keys ranging from 1..100
> And the value of each of these keys is another dict. What I need to do is make 3 smaller dicts and assign the values of keys 1..100 in the ratio 2:3:5.
> For example, if my original dict is
>
> d={"1":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
> "2":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
> "3":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
> "4":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"}}
> ...
> ...
> "100":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"}
>
> I need to have three dicts d1, d2, d3 with d1 containing the values of first 20 keys, d2 containing the values on next 30 keys and d3 containing the values of the next 50.
> Can you please help me with this?
>
You can get a list of the entries using the dict's .items method. It's
then a simple matter of slicing the list.
Note that dicts aren't ordered, i.e. its keys aren't in a fixed order,
so if you want then in a particular order, you'll need to sort the list.
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-10-14 21:57 -0400 |
| Message-ID | <mailman.14868.1413338052.18130.python-list@python.org> |
| In reply to | #79107 |
anuragpatibandla7@gmail.com Wrote in message:
> I have a dictionary that looks like this:
> {"1":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
> "2":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
> "3":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
> "4":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"}}
>
> Now if I have 100 objects like these and I need to split them into 3 smaller dicts in a ratio 2:3:5, how could I do that?
> I tried using numpy.random.choice(), but it says it needs to be a 1-d array.
>
What have you actually tried? You haven't shown any actual code.
Look up the method dict.keys, and see how you might use that. Then
look up random.shuffle, and see what it would do. Also look up
dict.sort, since your two messages on this thread imply two
conflicting goals as to which sub dictionaries should go in which
of your buckets.
As for extracting 20% of the keys, slicing is your answer. If
there are 100 keys, 20, 30, and 50 need to be sliced
off.
Then you'll need a loop to build each result dictionary from its keys.
There are shortcuts, but it's best to learn the fundamentals first.
Try writing the code. If it doesn’t all work show us what you've
tried, and what you think is wrong. And when you get an
exception, show the whole traceback, don't just paraphrase one
of the lines.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-10-14 19:32 -0700 |
| Message-ID | <97e450f8-26ec-4756-a52c-d5e1423b0769@googlegroups.com> |
| In reply to | #79124 |
On Wednesday, October 15, 2014 7:35:18 AM UTC+5:30, Dave Angel wrote:
> anurag Wrote in message:
> > I have a dictionary that looks like this:
> > {"1":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
> > "2":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
> > "3":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"},
> > "4":{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"}}
> > Now if I have 100 objects like these and I need to split them into 3 smaller dicts in a ratio 2:3:5, how could I do that?
> > I tried using numpy.random.choice(), but it says it needs to be a 1-d array.
> What have you actually tried? You haven't shown any actual code.
> Look up the method dict.keys, and see how you might use that. Then
> look up random.shuffle, and see what it would do. Also look up
> dict.sort, since your two messages on this thread imply two
> conflicting goals as to which sub dictionaries should go in which
> of your buckets.
> As for extracting 20% of the keys, slicing is your answer. If
> there are 100 keys, 20, 30, and 50 need to be sliced
> off.
> Then you'll need a loop to build each result dictionary from its keys.
> There are shortcuts, but it's best to learn the fundamentals first.
> Try writing the code. If it doesn't all work show us what you've
> tried, and what you think is wrong. And when you get an
> exception, show the whole traceback, don't just paraphrase one
> of the lines.
Yes that is what is in general expected out here -- code --
maybe working, maybe not, maybe incomplete, maybe 'pseudo' etc
Then others here will improve it
However there is one conceptual thing that perhaps should be mentioned: order.
Is your data *essentially* ordered?
And by 'essentially' I mean you think of it independent of python.
Yeah in python dicts are unordered and lists are ordered and one can fudge
one to behave a bit like the other. But before you fudge, please ponder which
you really need/want.
Below a bit of going from one to other
# dict -> list
>>> d = {"a":1,"b":2,"c":3}
>>> d
{'a': 1, 'c': 3, 'b': 2}
>>> list(d)
['a', 'c', 'b']
# alternate
>>> d.keys()
['a', 'c', 'b']
>>> d.items()
[('a', 1), ('c', 3), ('b', 2)]
>>> d.values()
[1, 3, 2]
# list -> dict
>>> dict(d.items())
{'a': 1, 'c': 3, 'b': 2}
# round-tripping
>>> dict(d.items()) == d
True
[toc] | [prev] | [next] | [standalone]
| From | Anurag Patibandla <anuragpatibandla7@gmail.com> |
|---|---|
| Date | 2014-10-14 20:40 -0700 |
| Message-ID | <7582dd3f-c0e1-4d5e-a03e-20bf711d1e79@googlegroups.com> |
| In reply to | #79107 |
Thanks for the response.
Here is the code that I have tried.
from operator import itemgetter
keys = json.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
for i in b:
queues = order[n:n+i]
n = n+i
print queues
for j in range(len(queues)):
q = (queues[j], json.get(queues[j]))
print q
By this I am able to get the 3 smaller dicts I want, but can you help me assign them to 3 variables?
The dicts need not be ordered but it would be better if they are ordered.
Thanks
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-10-14 21:50 -0700 |
| Message-ID | <32d89ca5-5e3f-47f4-945b-39d0ae5b40f8@googlegroups.com> |
| In reply to | #79132 |
On Wednesday, October 15, 2014 9:10:54 AM UTC+5:30, Anurag Patibandla wrote:
> Thanks for the response.
> Here is the code that I have tried.
> from operator import itemgetter
> keys = json.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
> for i in b:
> queues = order[n:n+i]
> n = n+i
> print queues
> for j in range(len(queues)):
> q = (queues[j], json.get(queues[j]))
> print q
Converting the end for loop (last 3 lines) into:
print [(queues[j], json.get(queues[j])) for j in range(len(queues))]
Does that help?
General advice:
1. Instead of writing 'naked' code as you have done, if you wrap it into
functions (preferably small)
2. Contents similar to the original naked code but with print's replaced by
return's
you make your as well as those trying to help/collaborate with you
life easier
Also the above is a more or mechanical translation. However
something[j] ... for j in range(len(something))
is usually a sign of a C programmer writing python :-)
Usually better to write
x for x in something
So...
Better to write that comprehension as
print [(q, json.get(q)) for q in queues]
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-10-15 13:43 -0400 |
| Message-ID | <mailman.14884.1413394863.18130.python-list@python.org> |
| In reply to | #79132 |
Anurag Patibandla <anuragpatibandla7@gmail.com> Wrote in message:
> Thanks for the response.
> Here is the code that I have tried.
>
> from operator import itemgetter
> keys = json.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
> print queues
>
> for j in range(len(queues)):
> q = (queues[j], json.get(queues[j]))
> print q
>
onedict = {}
for q in queues:
onedict[q] = json[q]
threedicts.append (onedict)
dict1, dictw, dict3 = threedicts
> By this I am able to get the 3 smaller dicts I want, but can you help me assign them to 3 variables?
> The dicts need not be ordered but it would be better if they are ordered.
>
dicts are not ordered. If you want the items in a particular
order, you have to do that after extraction from the dict. There
is a related type called collections.OrderedDict, which
'remembers' the order things were added.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Anurag Patibandla <anuragpatibandla7@gmail.com> |
|---|---|
| Date | 2014-10-15 11:35 -0700 |
| Message-ID | <7779f647-b676-47b6-82b4-d3024299cf7c@googlegroups.com> |
| In reply to | #79162 |
On Wednesday, October 15, 2014 1:41:13 PM UTC-4, Dave Angel wrote:
> Anurag Patibandla <anuragpatibandla7@gmail.com> Wrote in message:
>
> > Thanks for the response.
>
> > Here is the code that I have tried.
>
> >
>
> > from operator import itemgetter
>
> > keys = json.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
>
> > print queues
>
> >
>
> > for j in range(len(queues)):
>
> > q = (queues[j], json.get(queues[j]))
>
> > print q
>
> >
>
>
>
> onedict = {}
>
> for q in queues:
>
> onedict[q] = json[q]
>
> threedicts.append (onedict)
>
> dict1, dictw, dict3 = threedicts
>
>
>
> > By this I am able to get the 3 smaller dicts I want, but can you help me assign them to 3 variables?
>
> > The dicts need not be ordered but it would be better if they are ordered.
>
> >
>
>
>
> dicts are not ordered. If you want the items in a particular
>
> order, you have to do that after extraction from the dict. There
>
> is a related type called collections.OrderedDict, which
>
> 'remembers' the order things were added.
>
>
>
> --
>
> DaveA
Thanks DaveA!
This works perfectly!
[toc] | [prev] | [next] | [standalone]
| From | Anurag Patibandla <anuragpatibandla7@gmail.com> |
|---|---|
| Date | 2014-10-14 20:41 -0700 |
| Message-ID | <050847f8-713e-46cd-b18e-7c3fea6458f7@googlegroups.com> |
| In reply to | #79107 |
'json' has my original larger dict
[toc] | [prev] | [next] | [standalone]
| From | Anurag Patibandla <anuragpatibandla7@gmail.com> |
|---|---|
| Date | 2014-10-15 08:52 -0700 |
| Message-ID | <6004f171-689a-4a69-9b3d-2800fc4216d5@googlegroups.com> |
| In reply to | #79107 |
Thanks Rustom for the advice. I am new to Python and getting struck at some basic things. How do I assign the values that I am printing to 3 variables say dict1, dict2, dict3? When I try to assign them before the print statement like this: d1, d2, d3 =[(queues[j], json.get(queues[j])) for j in range(len(queues))] I get an error saying 'need more than one value to unpack'
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-10-15 09:06 -0700 |
| Message-ID | <07fd3d66-6343-4fe4-bb38-5d98eff4062d@googlegroups.com> |
| In reply to | #79149 |
On Wednesday, October 15, 2014 9:22:48 PM UTC+5:30, Anurag Patibandla wrote: > Thanks Rustom for the advice. > I am new to Python and getting struck at some basic things. How do I assign the values that I am printing to 3 variables say dict1, dict2, dict3? > When I try to assign them before the print statement like this: > d1, d2, d3 =[(queues[j], json.get(queues[j])) for j in range(len(queues))] > I get an error saying 'need more than one value to unpack' Probably means your comprehension [(queues[j], json.get(queues[j])) for j in range(len(queues))] is having less than 3 values >>> lst = [1,2,3] >>> x,y,z = lst >>> (x,y,z) # note no need to print (1, 2, 3) >>> lst=[1] >>> x,y,z=lst Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: need more than 1 value to unpack >>> lst=[1,2,3,4] >>> x,y,z=lst Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: too many values to unpack ================ I suggest youdont directly start with multiple assignment Instead do it in two steps dicts = [(queues[j], json.get(queues[j])) for j in range(len(queues))] d0 = dicts[0] d1 = dicts[1] d2 = dicts[2] When that works go to the more compact form Also please get rid of the range(len(queues)) Its unpythonic!
[toc] | [prev] | [next] | [standalone]
| From | Anurag Patibandla <anuragpatibandla7@gmail.com> |
|---|---|
| Date | 2014-10-15 09:28 -0700 |
| Message-ID | <26e61c1f-807c-4504-89b6-46771e0d51be@googlegroups.com> |
| In reply to | #79107 |
First the values printed by '[(queues[j], json.get(queues[j])) for j in range(len(queues))] ' is a list, so I tried to convert it into a dict using dict(). And then I tried doing dict[0] but there is an error which says: 'type' object has no attribute '__getitem__'
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-10-15 09:43 -0700 |
| Message-ID | <d2538693-924e-4eff-bad5-21cec5881fa5@googlegroups.com> |
| In reply to | #79152 |
On Wednesday, October 15, 2014 9:58:49 PM UTC+5:30, Anurag Patibandla wrote: > First the values printed by > '[(queues[j], json.get(queues[j])) for j in range(len(queues))] ' > is a list, so I tried to convert it into a dict using dict(). > And then I tried doing dict[0] but there is an error which says: > 'type' object has no attribute '__getitem__' print each step in the process until the error step Also assuming [(queues[j], json.get(queues[j])) for j in range(len(queues))] is the same as [(q, json.get(q) for q in queues] do a = [(q, json.get(q) for q in queues] print a b = dict(a) print b c = b[0] print c or whatever it is you are doing and PASTE (not NARRATE) the results
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-10-15 09:59 -0700 |
| Message-ID | <5c41697f-cb94-4803-b56e-d8d3909216cf@googlegroups.com> |
| In reply to | #79153 |
On Wednesday, October 15, 2014 10:13:18 PM UTC+5:30, Rustom Mody wrote: > On Wednesday, October 15, 2014 9:58:49 PM UTC+5:30, Anurag Patibandla wrote: > > First the values printed by > > '[(queues[j], json.get(queues[j])) for j in range(len(queues))] ' > > is a list, so I tried to convert it into a dict using dict(). > > And then I tried doing dict[0] but there is an error which says: > > 'type' object has no attribute '__getitem__' Also there are dictionary comprehensions recently added to python: http://legacy.python.org/dev/peps/pep-0274/ Can generally be used if you are doing dict(a list comprehension) To start with though, I suggest you stay with the longer older form until you understand how it works
[toc] | [prev] | [next] | [standalone]
| From | Anurag Patibandla <anuragpatibandla7@gmail.com> |
|---|---|
| Date | 2014-10-15 09:57 -0700 |
| Message-ID | <e1fa0eee-57d3-4935-9709-575d21267936@googlegroups.com> |
| In reply to | #79107 |
keys = json.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
for i in b:
queues = order[n:n+i]
n = n+i
#print queues
#print [(queues[j], json.get(queues[j])) for j in range(len(queues))]
lists = [(queues[j], json.get(queues[j])) for j in range(len(queues))]
#print lists
dicts = dict(lists)
print dicts
print dict[0]
Print dicts works as expected giving me the combine dictionary values. But when I say dict[0]. I see the error:
TypeError: 'type' object has no attribute '__getitem__'
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-10-15 13:48 -0400 |
| Message-ID | <mailman.14886.1413395410.18130.python-list@python.org> |
| In reply to | #79154 |
Anurag Patibandla <anuragpatibandla7@gmail.com> Wrote in message: > dicts = dict(lists) > print dicts > print dict[0] > > Print dicts works as expected giving me the combine dictionary values. But when I say dict[0]. I see the error: > TypeError: 'type' object has no attribute '__getitem__' > Of course. You forgot the s in the name dicts. So you were referring to the dict class, not your variable. -- DaveA
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web