Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5019

Re: Custom string joining

Path csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'does.': 0.07; "module's": 0.09; 'subject:string': 0.09; 'pm,': 0.11; 'skip:[ 20': 0.12; 'def': 0.13; 'wrote:': 0.14; 'iterable,': 0.16; 'likewise': 0.16; 'subject:joining': 0.16; 'argument': 0.16; 'intermediate': 0.16; 'cc:no real name:2**0': 0.20; 'usage': 0.20; 'cc:2**0': 0.20; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; 'mon,': 0.22; 'objects': 0.24; 'example': 0.24; 'memory': 0.24; 'version': 0.25; 'received:209.85.161.46': 0.26; 'received:mail- fx0-f46.google.com': 0.26; 'instead': 0.26; 'function': 0.27; 'message-id:@mail.gmail.com': 0.28; 'received:209.85.161': 0.29; 'string': 0.29; '(the': 0.30; 'list': 0.30; 'cc:addr:python.org': 0.31; 'objects.': 0.31; 'second': 0.31; 'does': 0.31; "skip:' 10": 0.32; 'uses': 0.34; 'print': 0.35; 'try:': 0.35; 'received:209.85': 0.37; 'received:google.com': 0.38; 'list:': 0.39; 'could': 0.39; 'received:209': 0.39; 'except': 0.39; 'similar': 0.40; 'would': 0.40; 'header:Received:5': 0.40; 'accepts': 0.60; 'further': 0.62; '2011': 0.62; 'subject:Custom': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type:content-transfer-encoding; bh=cMTRmTQHx1tdEVoLVCOK1Ffl85hnAcQ/k+r+Ykm1igU=; b=hJZg4r0fhaHM07FIQiGWdK5MvpT0BHE4+wq23LR4sBEGswJYebjCFBJzK8Gxm9rZ/7 0M1YSnpPRGJC+iWglZYzyvLyrGEXn230XHD2fRmdoCscchMEIN4IXcVvDFoLFwMsmIgW +1ltKfTnk/HMYzEJI6h1gGERF13diSa8grfS8=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=CULbLbboyuZ1fsX2tpFj7dOejB1pL3KIEY5/iMXtmdxZ/bcDHkh6v4E9wkGqF2klwK SIqhXH3NPU1nOpZyegWZFTfYkLE+Hy+p8pByAelnBJHReCQE83c6/8k/iZfoHIpP3Cyf CFwPxRRTfErwXScNyyb/8ZDO/jOfjzMD4DsJw=
MIME-Version 1.0
In-Reply-To <c1f15442-524b-406e-a074-e1a309bc3ec0@s16g2000prf.googlegroups.com>
References <mailman.1289.1304771900.9059.python-list@python.org> <c1f15442-524b-406e-a074-e1a309bc3ec0@s16g2000prf.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Mon, 9 May 2011 14:07:40 -0600
Subject Re: Custom string joining
To Martineau <ggrp2.20.martineau@dfgh.net>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1350.1304971691.9059.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 82.94.164.166
X-Trace 1304971692 news.xs4all.nl 81484 [::ffff:82.94.164.166]:58952
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5019

Show key headers only | View raw


On Mon, May 9, 2011 at 1:26 PM, Martineau <ggrp2.20.martineau@dfgh.net> wrote:
> Instead of join() here's a function that does something similar to
> what the string join() method does. The first argument can be a list
> of any type of objects and the second separator argument can likewise
> be any type. The result is list of the various objects. (The example
> usage just uses a list of string objects  and separator to illustrate
> what it does.)
>
> def tween(seq, sep):
>    return reduce(lambda r,v: r+[sep,v], seq[1:], seq[:1])
>
> lst = ['a','b','c','d','e']
>
> print tween(lst, '|')
> print ''.join(tween(lst, '|'))
>
> Output:
>
> ['a', '|', 'b', '|', 'c', '|', 'd', '|', 'e']
> a|b|c|d|e
>
>
> It could be made a little more memory efficient by applying the
> itertools module's islice() generator to the first 'seq' argument
> passed to reduce():
>
> def tween(seq, sep):
>    return reduce(lambda r,v: r+[sep,v], itertools.islice(seq,1,None),
> seq[:1])


This version accepts any iterable, not just a list:

def tween(seq, sep):
  it = iter(seq)
  try:
    first = it.next()
  except StopIteration:
    return []
  return reduce(lambda r, v: r + [sep, v], it, [first])

A further efficiency improvement would be to do the list concatenation
in place to avoid generating O(n) intermediate copies:

def tween(seq, sep):
  it = iter(seq)
  try:
    first = it.next()
  except StopIteration:
    return []
  def add_sep(r, v):
    r += [sep, v]
    return r
  return reduce(add_sep, it, [first])

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Custom string joining Claudiu Popa <cpopa@bitdefender.com> - 2011-05-07 15:31 +0300
  Re: Custom string joining Martineau <ggrp2.20.martineau@dfgh.net> - 2011-05-09 12:26 -0700
    Re: Custom string joining Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-09 14:07 -0600

csiph-web