Feature Proposal: Sequence .join method
David Murmann
david.murmann at rwth-aachen.de
Thu Sep 29 23:22:13 EDT 2005
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.
More information about the Python-list
mailing list