Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35030
| References | (2 earlier) <50CF64B8.6010305@tim.thechases.com> <50CF8FBC.9000100@lightbird.net> <50CF95FB.6060800@davea.name> <50CFA5E7.7020701@mrabarnett.plus.com> <CAHVvXxSfC5wC-iL=2w+GgRS=Ogujw-S1i68P9Phf70g5Zi3P2g@mail.gmail.com> |
|---|---|
| Date | 2012-12-18 00:22 +0000 |
| Subject | Re: Delete dict and subdict items of some name |
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1003.1355790142.29569.python-list@python.org> (permalink) |
On 17 December 2012 23:44, Oscar Benjamin <oscar.j.benjamin@gmail.com> wrote:
> On 17 December 2012 23:08, MRAB <python@mrabarnett.plus.com> wrote:
>> 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))
Slight correction:
dfs(g, lambda n: n.values(), lambda x: x.pop('Favicon', None))
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