Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35033
| Path | csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!multikabel.net!newsfeed20.multikabel.net!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <oscar.j.benjamin@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'patterns': 0.04; 'case.': 0.05; 'mrab': 0.05; 'try:': 0.07; 'val': 0.07; 'dict': 0.09; 'function:': 0.09; 'key)': 0.09; 'visited': 0.09; 'def': 0.10; "wouldn't": 0.11; ':-)': 0.13; 'd[key]': 0.16; 'indirect,': 0.16; 'key):': 0.16; 'key/value': 0.16; 'lambda': 0.16; 'node,': 0.16; 'reliably': 0.16; 'set()': 0.16; 'to:name:python list': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'tim': 0.18; '>>>': 0.18; 'regardless': 0.21; 'keyerror:': 0.22; 'header:In-Reply-To:1': 0.25; 'setting': 0.26; 'checking': 0.27; 'first,': 0.27; 'guess': 0.27; 'tree': 0.27; 'message-id:@mail.gmail.com': 0.27; '>>>>': 0.29; 'chase': 0.29; 'depth': 0.29; 'dictionary': 0.29; 'node': 0.29; 'search.': 0.29; 'subject:some': 0.29; 'no,': 0.29; 'objects': 0.29; 'expect': 0.31; 'december': 0.32; 'problem.': 0.32; 'to:addr:python-list': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; "didn't": 0.36; 'should': 0.36; 'possible': 0.37; 'one,': 0.37; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'delete': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'most': 0.61; 'first': 0.61; 'kind': 0.61; 'harder': 0.65; 'visiting': 0.79; "'if": 0.84; 'cycles.': 0.84; 'oscar': 0.84; 'do:': 0.91; 'angel': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=+fPRQZmdsWZ1cGbT1DkzejAy6YhyBFEaGao8zsV9Ieg=; b=DAVS37QA4z/QK0oYfZMCZu5e7SKpk6qdGJBeY1IjDf36CdYvTz00gk4ZLOdPbdDQ/j bmobS1jL/wGjTgDQlQYb/i9DO3ySZoqx4FvWthlpmL1sNGhsesQ9twI3UUGfWLpdJi8v 8ggwI4Ehh7tst+nbOfHc31W4rPmNScPxPj7LkEhyaMSBOtTRk85n6h37+WyL5bNVuv1J 9ssMdLNBASQDqTAlCTBip2X7Ji+4Tn5WWybw13JVnPnhuiEtvXdd7U/gtA8Bpe+DlCF7 pgILgcsAne6qPzVGRbNGWmsna9xo34PEbXvuCO26XyOnk0pDk3ICV5Q0BtA1j9i5Ve1J UxHQ== |
| MIME-Version | 1.0 |
| In-Reply-To | <50CFA5E7.7020701@mrabarnett.plus.com> |
| 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> <50CFA5E7.7020701@mrabarnett.plus.com> |
| Date | Mon, 17 Dec 2012 23:44:53 +0000 |
| Subject | Re: Delete dict and subdict items of some name |
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| To | Python List <python-list@python.org> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1006.1355793957.29569.python-list@python.org> (permalink) |
| Lines | 77 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1355793957 news.xs4all.nl 6976 [2001:888:2000:d::a6]:33659 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:35033 |
Show key headers only | View raw
On 17 December 2012 23:08, MRAB <python@mrabarnett.plus.com> wrote:
> On 2012-12-17 22:00, 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.
>>
> Wouldn't a set of the id of the visited objects work?
Of course it would. This is just a tree search.
Here's a depth-first-search function:
def dfs(root, childfunc, func):
'''depth first search on a tree
calls func(node) once for each node'''
visited = set()
visiting = OrderedDict()
visiting[id(root)] = it = iter([root])
while True:
try:
node = next(it)
except StopIteration:
try:
node, it = visiting.popitem()
except KeyError:
return
key = id(node)
if isinstance(node, dict) and key not in visited:
func(node)
visiting[key] = it = iter(childfunc(node))
visited.add(key)
Now you can do:
dfs(my_dict_tree, lambda x: x.pop('Favicon', None))
Although I wouldn't bother with the above unless I had some reason to
expect possible cycles.
Oscar
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Delete dict and subdict items of some name Gnarlodious <gnarlodious@gmail.com> - 2012-12-17 09:27 -0800
Re: Delete dict and subdict items of some name Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-12-17 17:42 +0000
Re: Delete dict and subdict items of some name Mitya Sirenef <msirenef@lightbird.net> - 2012-12-17 12:43 -0500
Re: Delete dict and subdict items of some name Paul Rubin <no.email@nospam.invalid> - 2012-12-17 09:48 -0800
Re: Delete dict and subdict items of some name Dave Angel <d@davea.name> - 2012-12-17 12:50 -0500
Re: Delete dict and subdict items of some name Tim Chase <python.list@tim.thechases.com> - 2012-12-17 12:30 -0600
Re: Delete dict and subdict items of some name Mitya Sirenef <msirenef@lightbird.net> - 2012-12-17 16:33 -0500
Re: Delete dict and subdict items of some name Chris Angelico <rosuav@gmail.com> - 2012-12-18 08:53 +1100
Re: Delete dict and subdict items of some name Dave Angel <d@davea.name> - 2012-12-17 17:00 -0500
Re: Delete dict and subdict items of some name MRAB <python@mrabarnett.plus.com> - 2012-12-17 23:08 +0000
Re: Delete dict and subdict items of some name Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-18 00:17 +0000
Re: Delete dict and subdict items of some name Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-12-18 00:22 +0000
Re: Delete dict and subdict items of some name Mitya Sirenef <msirenef@lightbird.net> - 2012-12-17 20:09 -0500
Re: Delete dict and subdict items of some name Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-12-17 23:44 +0000
Re: Delete dict and subdict items of some name Dave Angel <d@davea.name> - 2012-12-17 21:13 -0500
Re: Delete dict and subdict items of some name Gnarlodious <gnarlodious@gmail.com> - 2012-12-17 21:30 -0800
Re: Delete dict and subdict items of some name Hans Mulder <hansmu@xs4all.nl> - 2012-12-18 11:31 +0100
Re: Delete dict and subdict items of some name Gnarlodious <gnarlodious@gmail.com> - 2012-12-18 07:27 -0800
Re: Delete dict and subdict items of some name Terry Reedy <tjreedy@udel.edu> - 2012-12-18 11:29 -0500
csiph-web