Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #84992
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: parsing tree from excel sheet |
| Date | 2015-02-01 11:11 +0100 |
| Organization | None |
| References | <cirqviF15qtU1@mid.individual.net> <mailman.18217.1422454096.18130.python-list@python.org> <cj1kt9Fi3e0U1@mid.individual.net> <mailman.18314.1422637907.18130.python-list@python.org> <cj547rFfl2sU1@mid.individual.net> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.18356.1422785505.18130.python-list@python.org> (permalink) |
alb wrote: >> But wait, "".join() only accepts strings so let's change >> >>yield [node] >> >> to >>yield [node.name] # str(node) would also work > > Again my question, why not simply yield node.name? I've been conditioned to build a string from many substrings like so >>> parts = ["foo", "bar", "baz"] >>> text = "".join(parts) >>> text 'foobarbaz' instead of >>> text = "" >>> for part in parts: ... text += part ... >>> text 'foobarbaz' mostly because ""join(...) was initially advertised as being faster than +=. For nested generators or functions this translates into >>> def outer_list(): ... return ["foo"] + inner_list() ... >>> def inner_list(): ... return ["bar"] ... >>> "".join(outer_list()) 'foobar' instead of the obvious >>> def outer(): ... return "foo" + inner() ... >>> def inner(): ... return "bar" ... >>> outer() 'foobar' Here the list-based approach may build many intermediate throwaway-lists, so it's most likely less efficient than direct string concatenation. In return it gives you flexibility: >>> "/".join(outer_list()) 'foo/bar' >>> "-->".join(outer_list()) 'foo-->bar' You'd have to pass the separator as an argument to outer/inner() to achieve this with the seemingly simpler approach. But there's more: You can reverse the order, >>> ":".join(reversed(outer_list())) 'bar:foo' treat the innermost string differently, translate each part into a different language, clip common levels, etc. Your options are unlimited. I never understood why the file system API operates on a single string...
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-28 10:12 +0000
Re: parsing tree from excel sheet Peter Otten <__peter__@web.de> - 2015-01-28 15:08 +0100
Re: parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-28 14:27 +0000
Re: parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-29 21:02 +0000
Re: parsing tree from excel sheet MRAB <python@mrabarnett.plus.com> - 2015-01-29 21:16 +0000
Re: parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-29 21:32 +0000
Re: parsing tree from excel sheet Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-29 21:59 +0000
Re: parsing tree from excel sheet Chris Kaynor <ckaynor@zindagigames.com> - 2015-01-29 14:30 -0800
Re: parsing tree from excel sheet Chris Angelico <rosuav@gmail.com> - 2015-01-30 10:46 +1100
Re: parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-30 15:05 +0000
Re: parsing tree from excel sheet Peter Otten <__peter__@web.de> - 2015-01-30 18:11 +0100
Re: parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-31 22:45 +0000
Re: parsing tree from excel sheet Peter Otten <__peter__@web.de> - 2015-02-01 11:11 +0100
Re: parsing tree from excel sheet Peter Otten <__peter__@web.de> - 2015-01-30 18:24 +0100
Re: parsing tree from excel sheet Peter Otten <__peter__@web.de> - 2015-01-31 10:07 +0100
Re: parsing tree from excel sheet Peter Otten <__peter__@web.de> - 2015-01-31 10:07 +0100
Re: parsing tree from excel sheet Tim Chase <python.list@tim.thechases.com> - 2015-01-28 08:13 -0600
Re: parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-29 21:22 +0000
csiph-web