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


Groups > comp.lang.python > #4907 > unrolled thread

Custom string joining

Started byClaudiu Popa <cpopa@bitdefender.com>
First post2011-05-07 15:31 +0300
Last post2011-05-09 14:07 -0600
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#4907 — Custom string joining

FromClaudiu Popa <cpopa@bitdefender.com>
Date2011-05-07 15:31 +0300
SubjectCustom string joining
Message-ID<mailman.1289.1304771900.9059.python-list@python.org>
Hello Python-list,

I  have  an object which defines some methods. I want to join a list or
an iterable of those objects like this:

new_string = "|".join(iterable_of_custom_objects)

What   is   the   __magic__  function that needs to be implemented for
this case to work?  I  though  that  __str__  is sufficient but it doesn't seems to
work. Thanks in advance.

PC

  


[toc] | [next] | [standalone]


#5014

FromMartineau <ggrp2.20.martineau@dfgh.net>
Date2011-05-09 12:26 -0700
Message-ID<c1f15442-524b-406e-a074-e1a309bc3ec0@s16g2000prf.googlegroups.com>
In reply to#4907
On May 7, 5:31 am, Claudiu Popa <cp...@bitdefender.com> wrote:
> Hello Python-list,
>
> I  have  an object which defines some methods. I want to join a list or
> an iterable of those objects like this:
>
> new_string = "|".join(iterable_of_custom_objects)
>
> What   is   the   __magic__  function that needs to be implemented for
> this case to work?  I  though  that  __str__  is sufficient but it doesn't seems to
> work. Thanks in advance.
>
> PC

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])


--
Martin

[toc] | [prev] | [next] | [standalone]


#5019

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-05-09 14:07 -0600
Message-ID<mailman.1350.1304971691.9059.python-list@python.org>
In reply to#5014
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])

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web