Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53941
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-09-10 20:52 -0700 |
| References | <33650fe9-802e-477d-a361-6d4bdeb28762@googlegroups.com> <522fd1ee$0$29988$c3e8da3$5496439d@news.astraweb.com> <mailman.231.1378866311.5461.python-list@python.org> <522fdfec$0$29988$c3e8da3$5496439d@news.astraweb.com> |
| Message-ID | <1ef6a37a-e888-4ecc-a82a-477e19490723@googlegroups.com> (permalink) |
| Subject | Re: Dealing with Lists |
| From | stas poritskiy <stascrash@gmail.com> |
Steven,
i think you got on the right track with your proposal,
although i am not going after the "visual" represenatation that you were able to create, rather a structural one, i think this might work for me,
instead of printing, i could be using my commands to make elements (instances of API objects to create my groups inside the file)
but one question i might have is :
once i created first instance object which in my example is :
groups = intance.add_group(str(name))
how would i temporary preserve this name as a reference for the next iteration for the subroups?
On Tuesday, September 10, 2013 10:13:48 PM UTC-5, Steven D'Aprano wrote:
> On Wed, 11 Sep 2013 02:24:44 +0000, Dave Angel wrote:
>
>
>
> > On 10/9/2013 22:14, Steven D'Aprano wrote:
>
> >
>
> >> On Tue, 10 Sep 2013 14:08:45 -0700, stas poritskiy wrote:
>
> >>
>
> >>> Greetings to all!
>
> >>>
>
> >>> i ran into a little logic problem and trying to figure it out.
>
> >>>
>
> >>> my case is as follows:
>
> >>>
>
> >>> i have a list of items each item represents a Group i need to create a
>
> >>> set of nested groups, so, for example:
>
> >>>
>
> >>> myGroups = ["head", "neck", "arms", "legs"]
>
> >>
>
> >>
>
> >> What is the rule for grouping these items? Below, you suggest:
>
> >>
>
> >> head encloses neck
>
> >> neck encloses arms
>
> >> arms encloses legs
>
> >>
>
> >> which seems rather strange. But if it is *always* the case that each
>
> >> item encloses the next item:
>
> >>
>
> >> def print_nested_list(alist):
>
> >> spaces = ' '*4
>
> >> for level, item in enumerate(alist):
>
> >> if level != 0:
>
> >> indent = spaces*(level-1) + ' '
>
> >> print (indent + '|_>'), # note comma
>
> >> print item
>
> >>
>
> >>
>
> >> which gives us this:
>
> >>
>
> >> py> print_nested_list(['head', 'neck', 'arms', 'legs']) head
>
> >> |_> neck
>
> >> |_> arms
>
> >> |_> legs
>
> >>
>
> >>
>
> >> as requested.
>
> >>
>
> >>
>
> >>
>
> > Very nice. But what I want to know is how did you know that Stan (the
>
> > OP) wanted his printed output to be formatted that way?
>
>
>
> I don't. Stan's email is unclear. But he does show an example:
>
>
>
> [quote]
>
> so, for example:
>
>
>
> myGroups = ["head", "neck", "arms", "legs"]
>
>
>
> i need to get them to be represented like this: (if you can imaging a
>
> folder structure)
>
>
>
> head
>
> |_> neck
>
> |_> arms
>
> |_>legs
>
>
>
> and so on until i hit the last element.
>
> [end quote]
>
>
>
>
>
> so I just provided that.
>
>
>
> > He said:
>
> >
>
> >>>>>> i need to create a set of nested groups,
>
> > and
>
> >>>>>> store each of the first elements of a par, so I can reference to
>
> >>>>>> them as to a parent of another group.
>
>
>
>
>
> I have no idea what that means :-)
>
>
>
>
>
> I *guess* that what Stan is actually looking for is something like a
>
> dictionary-based solution, not lists:
>
>
>
> {'head': {'neck': {'arms': {}, 'legs': {}}}}
>
>
>
> which gives:
>
>
>
> head encloses neck
>
> neck encloses arms and legs
>
> arms enclose nothing
>
> legs enclose nothing
>
>
>
>
>
> or:
>
>
>
> {'head': {'neck': {'arms': {'legs': {}}}}}
>
>
>
> which gives:
>
>
>
> head encloses neck
>
> neck encloses arms
>
> arms encloses legs
>
> legs enclose nothing
>
>
>
>
>
> but I can't really tell for sure. In this second case, using dicts, I
>
> might try something like this recursive solution:
>
>
>
>
>
> def print_nested_dict(adict, level=0):
>
> if adict == {}:
>
> return
>
> for key, subdict in sorted(adict.items()):
>
> if level != 0:
>
> spaces = ' '*4
>
> indent = spaces*(level-1) + ' '
>
> print (indent + '|_>'), # note comma
>
> if subdict == {}:
>
> print key
>
> else:
>
> print "%s:-" % key
>
> print_nested_dict(subdict, level+1)
>
>
>
>
>
> Given:
>
>
>
> d = {'head': {'neck': {'legs': {'toes': {}}, 'arms': {'thumbs': {},
>
> 'fingers': {}}}}}
>
>
>
> we can see this output:
>
>
>
> py> print_nested_dict(d)
>
> head:-
>
> |_> neck:-
>
> |_> arms:-
>
> |_> fingers
>
> |_> thumbs
>
> |_> legs:-
>
> |_> toes
>
>
>
>
>
>
>
>
>
> --
>
> Steven
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-10 14:08 -0700
Re: Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-10 14:10 -0700
Re: Dealing with Lists matt.komyanek@gmail.com - 2013-09-10 14:31 -0700
Re: Dealing with Lists Dave Angel <davea@davea.name> - 2013-09-10 21:51 +0000
Re: Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-10 15:11 -0700
Re: Dealing with Lists Roy Smith <roy@panix.com> - 2013-09-10 18:30 -0400
Re: Dealing with Lists Dave Angel <davea@davea.name> - 2013-09-10 22:32 +0000
Re: Dealing with Lists Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-11 02:14 +0000
Re: Dealing with Lists Dave Angel <davea@davea.name> - 2013-09-11 02:24 +0000
Re: Dealing with Lists Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-11 03:13 +0000
Re: Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-10 20:44 -0700
Re: Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-10 20:52 -0700
Re: Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-11 07:47 -0700
Re: Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-11 07:57 -0700
Re: Dealing with Lists Peter Otten <__peter__@web.de> - 2013-09-11 18:17 +0200
csiph-web