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


Groups > comp.lang.python > #17302

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'mrab': 0.04; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; 'converting': 0.15; "'a'": 0.16; "(i'll": 0.16; 'bit.': 0.16; 'comma': 0.16; 'comma,': 0.16; "doesn't.": 0.16; 'handle,': 0.16; 'iterator': 0.16; 'iterators,': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'roy': 0.16; 'settle': 0.16; 'wrote:': 0.18; 'string,': 0.18; 'seems': 0.20; 'header:In-Reply-To:1': 0.22; '(where': 0.23; 'code': 0.25; 'bit': 0.28; 'odd': 0.29; 'handling': 0.30; "i've": 0.31; 'list': 0.32; 'pretty': 0.32; 'header:User-Agent:1': 0.33; 'instead': 0.33; 'there': 0.33; 'it?': 0.33; 'to:addr:python-list': 0.34; 'running': 0.35; 'especially': 0.35; 'subject:How': 0.35; 'beginning': 0.36; 'conjunction': 0.37; 'but': 0.37; 'list,': 0.37; "there's": 0.37; 'some': 0.38; 'why': 0.39; "it's": 0.40; 'to:addr:python.org': 0.40; 'more': 0.61; 'kind': 0.61; 'below': 0.63; 'received:websitewelcome.com': 0.64; 'received:184': 0.67; 'special': 0.68; 'suggests.': 0.84
Date Thu, 15 Dec 2011 10:19:51 -0800
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Thunderbird 1.5.0.10 (Windows/20070221)
MIME-Version 1.0
To Python <python-list@python.org>
Subject Re: How to generate "a, b, c, and d"?
References <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA35F8.4090502@tim.thechases.com>
In-Reply-To <4EEA35F8.4090502@tim.thechases.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - gator410.hostgator.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - stoneleaf.us
X-BWhitelist no
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:1774
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 1
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ==
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.3689.1323974099.27778.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323974099 news.xs4all.nl 6844 [2001:888:2000:d::a6]:60211
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:17302

Show key headers only | View raw


Tim Chase wrote:
> 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

<snip>

Why go through all that instead of just converting the iterator into a 
list at the beginning of MRAB's solution and then running with it?

~Ethan~

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