Re: Feature Proposal: Sequence .join method
Michele Simionato [EMAIL PROTECTED] wrote: ... I have noticed a while ago that inside generators StopIteration is automatically trapped, i.e. def g(): yield 1 raise StopIteration yield Never reached only yields 1. Not sure if this is documented behavior, however, of if it is an implementation accident. Anybody who knows? It yields 1 and on the next call to .next() raises StopIteration, which is the way an iterator says it's done -- so, it yields 1 and then it's done. I'm not sure what you mean by inside generators ... automatically trapped, or what's undocumented about that. Alex -- http://mail.python.org/mailman/listinfo/python-list
Re: Feature Proposal: Sequence .join method
David Murmann [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] def join(sep, seq): return reduce(lambda x, y: x + sep + y, seq, type(sep)()) damn, i wanted too much. Proper implementation: def join(sep, seq): if len(seq): return reduce(lambda x, y: x + sep + y, seq) return type(sep)() but still short enough For general use, this is both too general and not general enough. If len(seq) exists then seq is probably reiterable, in which case it may be possible to determine the output length and preallocate to make the process O(n) instead of O(n**2). I believe str.join does this. A user written join for lists could also. A tuple function could make a list first and then tuple(it) at the end. If seq is a general (non-empty) iterable, len(seq) may raise an exception even though the reduce would work fine. Terry J. Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Feature Proposal: Sequence .join method
On Fri, 30 Sep 2005 09:38:25 -0700, Michael Spencer [EMAIL PROTECTED] wrote: Terry Reedy wrote: David Murmann [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] def join(sep, seq): return reduce(lambda x, y: x + sep + y, seq, type(sep)()) damn, i wanted too much. Proper implementation: def join(sep, seq): if len(seq): return reduce(lambda x, y: x + sep + y, seq) return type(sep)() but still short enough For general use, this is both too general and not general enough. If len(seq) exists then seq is probably reiterable, in which case it may be possible to determine the output length and preallocate to make the process O(n) instead of O(n**2). I believe str.join does this. A user written join for lists could also. A tuple function could make a list first and then tuple(it) at the end. If seq is a general (non-empty) iterable, len(seq) may raise an exception even though the reduce would work fine. Terry J. Reedy For the general iterable case, you could have something like this: def interleave(sep, iterable): ... it = iter(iterable) ... next = it.next() ... try: ... while 1: ... item = next ... next = it.next() ... yield item ... yield sep ... except StopIteration: ... yield item ... list(interleave(100,range(10))) [0, 100, 1, 100, 2, 100, 3, 100, 4, 100, 5, 100, 6, 100, 7, 100, 8, 100, 9] but I can't think of a use for it ;-) I have this version: def interlace(x, i): interlace(x, i) - i0, x, i1, x, ..., x, iN i = iter(i) try: yield i.next() except StopIteration: return for e in i: yield x yield e And I use it for things like interleave(, , [foo, bar, baz]), where bar is not a string, but can be handled along with strings by a lower-level chunk of code. Jp -- http://mail.python.org/mailman/listinfo/python-list
Re: Feature Proposal: Sequence .join method
David Murmann wrote: replace the line result = result + self + T(item) with result = result + self + item and of course the line result = T(seq[0]) with result = seq[0] -- http://mail.python.org/mailman/listinfo/python-list
Re: Feature Proposal: Sequence .join method
David Murmann wrote: Hi all! I could not find out whether this has been proposed before (there are too many discussion on join as a sequence method with different semantics). So, i propose a generalized .join method on all sequences with these semantics: def join(self, seq): T = type(self) result = T() if len(seq): result = T(seq[0]) for item in seq[1:]: result = result + self + T(item) return result This would allow code like the following: [0].join([[5], [42, 5], [1, 2, 3], [23]]) I don't like the idea of having to put this on all sequences. If you want this, I'd instead propose it as a function (perhaps builtin, perhaps in some other module). Also, this particular implementation is a bad idea. The repeated += to result is likely to result in O(N**2) behavior. STeVe -- http://mail.python.org/mailman/listinfo/python-list
Re: Feature Proposal: Sequence .join method
Steven Bethard wrote: David Murmann wrote: Hi all! I could not find out whether this has been proposed before (there are too many discussion on join as a sequence method with different semantics). So, i propose a generalized .join method on all sequences with these semantics: def join(self, seq): T = type(self) result = T() if len(seq): result = T(seq[0]) for item in seq[1:]: result = result + self + T(item) return result This would allow code like the following: [0].join([[5], [42, 5], [1, 2, 3], [23]]) I don't like the idea of having to put this on all sequences. If you want this, I'd instead propose it as a function (perhaps builtin, perhaps in some other module). Also, this particular implementation is a bad idea. The repeated += to result is likely to result in O(N**2) behavior. STeVe Hi and thanks for the fast reply, i just figured out that the following implementation is probably much faster, and short enough to be used in place for every of my use cases: def join(sep, seq): return reduce(lambda x, y: x + sep + y, seq, type(sep)()) so, i'm withdrawing my proposal, and instead propose to keep reduce and lambda in py3k ;). thanks again, David. -- http://mail.python.org/mailman/listinfo/python-list
Re: Feature Proposal: Sequence .join method
def join(sep, seq): return reduce(lambda x, y: x + sep + y, seq, type(sep)()) damn, i wanted too much. Proper implementation: def join(sep, seq): if len(seq): return reduce(lambda x, y: x + sep + y, seq) return type(sep)() but still short enough see you, David. -- http://mail.python.org/mailman/listinfo/python-list
Re: Feature Proposal: Sequence .join method
David Murmann wrote: I could not find out whether this has been proposed before (there are too many discussion on join as a sequence method with different semantics). So, i propose a generalized .join method on all sequences so all you have to do now is to find the sequence base class, and you're done... /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Feature Proposal: Sequence .join method
On Thu, 29 Sep 2005 20:37:31 -0600 Steven Bethard wrote: I don't like the idea of having to put this on all sequences. If you want this, I'd instead propose it as a function (perhaps builtin, perhaps in some other module). itertools module seems the right place for it. itertools.chain(*a) is the same as the proposed [].join(a) -- jk -- http://mail.python.org/mailman/listinfo/python-list