Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!news.muarf.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.87.MISMATCH!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'that?': 0.05; 'subject:Python': 0.06; 'assign': 0.07; 'method.': 0.07; 'keys,': 0.09; 'python': 0.11; 'wrote': 0.14; 'array.': 0.16; 'dict': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'means.': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'sequence.': 0.16; 'wrote:': 0.18; 'hey': 0.18; 'split': 0.19; 'example': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; "aren't": 0.24; 'entries': 0.24; 'skip:{ 20': 0.24; 'specify': 0.24; 'looks': 0.24; 'sort': 0.25; 'this:': 0.26; 'skip:" 20': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'idea': 0.28; 'fixed': 0.29; 'keys': 0.31; 'update.': 0.31; 'yes.': 0.31; 'another': 0.32; 'says': 0.33; 'skip:d 20': 0.34; 'could': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'objects': 0.35; 'received:84': 0.35; 'but': 0.35; 'really': 0.36; '14,': 0.36; 'i.e.': 0.36; 'next': 0.36; 'example,': 0.37; 'so,': 0.37; 'list': 0.37; 'list.': 0.37; 'version,': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'dave': 0.60; 'matter': 0.61; 'simple': 0.61; 'first': 0.61; "you'll": 0.62; 'email addr:gmail.com': 0.63; 'more': 0.64; 'statement,': 0.68; 'containing': 0.69; '100': 0.79; '2.7.': 0.84; 'clearer': 0.84; 'dict.': 0.84; 'angel': 0.91; 'ratio': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=Ar9g3YNP c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=m9fHe4oz-9sA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=pGLkceISAAAA:8 a=-ttYdMmXyD6y4vDgTMEA:9 a=QEXdDO2ut3YA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Tue, 14 Oct 2014 23:58:25 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.1.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Parsing Python dictionary with multiple objects References: <64f7e57a-4be3-44f4-b11c-0d270fc91809@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413327514 news.xs4all.nl 2972 [2001:888:2000:d::a6]:47814 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79121 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.