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


Groups > comp.lang.python > #17290

Re: How to generate "a, b, c, and d"?

Date 2011-12-15 12:01 -0600
From Tim Chase <python.list@tim.thechases.com>
Subject Re: How to generate "a, b, c, and d"?
References <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2>
Newsgroups comp.lang.python
Message-ID <mailman.3680.1323972100.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 12/15/11 10:48, Roy Smith wrote:
> I've got a list, ['a', 'b', 'c', 'd'].  I want to generate the string, "a, b, c, and d" (I'll settle for no comma after 'c').  Is there some standard way to do this, handling all the special cases?
>
> [] ==>  ''
> ['a'] ==>  'a'
> ['a', 'b'] ==>  'a and b'
> ['a', 'b', 'c', 'd'] ==>  'a, b, and c'
>
> It seems like the kind of thing django.contrib.humanize would handle, but alas, it doesn't.

If you have a list, it's pretty easy as MRAB suggests.  For 
arbitrary iterators, it's a bit more complex.  Especially with 
the odd edge-case of 2 items where there's no comma before the 
conjunction (where >2 has the comma before the conjunction).  If 
you were willing to forgo the Oxford comma, it would tidy up the 
code a bit.  Sample code below

-tkc

def gen_list(i, conjunction="and"):
     i = iter(i)
     first = i.next()
     try:
         prev = i.next()
     except StopIteration:
         yield first
     else:
         more_than_two = False
         for item in i:
             if not more_than_two: yield first
             yield prev
             prev = item
             more_than_two = True
         if more_than_two:
             yield "%s %s" % (conjunction, prev)
         else:
             yield "%s %s %s" % (first, conjunction, prev)

def listify(lst, conjunction="and"):
     return ', '.join(gen_list(lst, conjunction))

for test, expected in (
         ([], ''),
         (['a'], 'a'),
         (['a', 'b'], 'a and b'),
         (['a', 'b', 'c'], 'a, b, and c'),
         (['a', 'b', 'c', 'd'], 'a, b, c, and d'),
         ):
     result = listify(test)
     print "%r -> %r (got %r) %s" % (
         test, expected, result,
         result == expected and "PASSED" or "FAILED"
         )

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


Thread

How to generate "a, b, c, and d"? Roy Smith <roy@panix.com> - 2011-12-15 08:48 -0800
  Re: How to generate "a, b, c, and d"? MRAB <python@mrabarnett.plus.com> - 2011-12-15 17:27 +0000
  Re: How to generate "a, b, c, and d"? Tim Chase <python.list@tim.thechases.com> - 2011-12-15 12:01 -0600
  Re: How to generate "a, b, c, and d"? Ethan Furman <ethan@stoneleaf.us> - 2011-12-15 10:19 -0800
  Re: How to generate "a, b, c, and d"? Tim Chase <python.list@tim.thechases.com> - 2011-12-15 12:51 -0600
    Re: How to generate "a, b, c, and d"? Roy Smith <roy@panix.com> - 2011-12-15 11:01 -0800
    Re: How to generate "a, b, c, and d"? Roy Smith <roy@panix.com> - 2011-12-15 11:01 -0800
  Re: How to generate "a, b, c, and d"? MRAB <python@mrabarnett.plus.com> - 2011-12-15 19:27 +0000
  Re: How to generate "a, b, c, and d"? Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-15 14:22 -0700
  Re: How to generate "a, b, c, and d"? Terry Reedy <tjreedy@udel.edu> - 2011-12-15 19:57 -0500
  Re: How to generate "a, b, c, and d"? Chris Angelico <rosuav@gmail.com> - 2011-12-16 13:42 +1100
  Re: How to generate "a, b, c, and d"? Terry Reedy <tjreedy@udel.edu> - 2011-12-16 00:26 -0500

csiph-web