Re:Parsing Python dictionary with multiple objects

2014-10-14 Thread Dave Angel
anuragpatiband...@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

-- 
https://mail.python.org/mailman/listinfo/python-list


Re:Parsing Python dictionary with multiple objects

2014-10-14 Thread Dave Angel
anuragpatiband...@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

-- 
https://mail.python.org/mailman/listinfo/python-list