Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53938
| From | Dave Angel <davea@davea.name> |
|---|---|
| Subject | Re: Dealing with Lists |
| Date | 2013-09-11 02:24 +0000 |
| References | <33650fe9-802e-477d-a361-6d4bdeb28762@googlegroups.com> <522fd1ee$0$29988$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.231.1378866311.5461.python-list@python.org> (permalink) |
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? 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. -- DaveA
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