Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'patterns': 0.04; 'case.': 0.05; 'processed': 0.05; 'level,': 0.07; 'linear': 0.07; 'present,': 0.07; 'val': 0.07; 'already.': 0.09; 'dict': 0.09; 'iterate': 0.09; 'key)': 0.09; 'loop.': 0.09; 'path,': 0.09; 'python:': 0.09; 'def': 0.10; ':-)': 0.13; 'algorithm.': 0.16; 'd[key]': 0.16; 'indirect,': 0.16; 'instantiate': 0.16; 'iterator': 0.16; 'key):': 0.16; 'key/value': 0.16; 'nodes': 0.16; 'quit.': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'reliably': 0.16; 'twice.': 0.16; 'wrote:': 0.17; 'thanks,': 0.18; 'tim': 0.18; '>>>': 0.18; 'memory': 0.18; 'regardless': 0.21; 'so.': 0.24; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'setting': 0.26; 'checking': 0.27; 'first,': 0.27; 'guess': 0.27; 'object,': 0.27; "doesn't": 0.28; '>>>>': 0.29; 'chase': 0.29; 'depth': 0.29; 'dictionary': 0.29; 'once,': 0.29; 'subject:some': 0.29; 'no,': 0.29; 'point': 0.31; 'problem.': 0.32; 'to:addr :python-list': 0.33; 'another': 0.33; 'list': 0.35; 'path': 0.35; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'compare': 0.36; "didn't": 0.36; 'should': 0.36; 'one,': 0.37; 'two': 0.37; 'quite': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'delete': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'build': 0.39; 'received:192.168': 0.40; 'think': 0.40; 'most': 0.61; 'kind': 0.61; 'ever': 0.63; 'times': 0.63; 'harder': 0.65; "'if": 0.84; 'tricky': 0.91; 'angel': 0.93 Date: Mon, 17 Dec 2012 20:09:01 -0500 From: Mitya Sirenef User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Delete dict and subdict items of some name References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <50CF59DE.6080608@lightbird.net> <50CF64B8.6010305@tim.thechases.com> <50CF8FBC.9000100@lightbird.net> <50CF95FB.6060800@davea.name> In-Reply-To: <50CF95FB.6060800@davea.name> Content-Type: text/plain; charset=ISO-8859-1; 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355793018 news.xs4all.nl 6844 [2001:888:2000:d::a6]:59041 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35032 On 12/17/2012 05:00 PM, Dave Angel wrote: > On 12/17/2012 04:33 PM, Mitya Sirenef wrote: >> On 12/17/2012 01:30 PM, Tim Chase wrote: >>> On 12/17/12 11:43, Mitya Sirenef wrote: >>>> On 12/17/2012 12:27 PM, Gnarlodious wrote: >>>>> Hello. What I want to do is delete every dictionary key/value >>>>> of the name 'Favicon' regardless of depth in subdicts, of which >>>>> there are many. What is the best way to do it? >>>> Something like this should work: >>>> >>>> def delkey(d, key): >>>> if isinstance(d, dict): >>>> if key in d: del d[key] >>>> for val in d.values(): >>>> delkey(val, key) >>> Unless you have something hatefully recursive like >>> >>> d = {} >>> d["hello"] = d >>> >>> :-) >> True -- didn't think of that..! >> >> I guess then adding a check 'if val is not d: delkey(val, key)' >> would take care of it? >> > No, that would only cover the self-recursive case. If there's a dict > which contains another one, which contains the first, then the recursion > is indirect, and much harder to check for. > > Checking reliably for arbitrary recursion patterns is tricky, but > do-able. Most people degenerate into just setting an arbitrary max > depth. But I can describe two approaches to this kind of problem. > > 1) build a list of the recursion path at present, and compare against > the whole path, rather than just the tail. If there are any matches, quit. > > 2) make the iterator an object, and instantiate two of them. Then each > recursive level, iterate the main one once, and the secondary one > twice. If the two ever match, you have a loop. Deciding what to do at > that point is tricky because you may have processed some nodes multiple > times already. But at least it'll terminate, and it doesn't use linear > memory to do so. I call this one the lollypop algorithm. > > Thanks, this is quite interesting.. -- Lark's Tongue Guide to Python: http://lightbird.net/larks/