Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'mrab': 0.04; 'test,': 0.04; '%s"': 0.09; 'def': 0.13; 'result,': 0.15; '"%s': 0.16; "''),": 0.16; "'a'": 0.16; "(i'll": 0.16; '-tkc': 0.16; 'bit.': 0.16; 'comma': 0.16; 'comma,': 0.16; "doesn't.": 0.16; 'expected,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'handle,': 0.16; 'iterators,': 0.16; 'message- id:@tim.thechases.com': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 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; 'cc:2**0': 0.24; 'code': 0.25; 'bit': 0.28; 'odd': 0.29; 'yield': 0.29; 'print': 0.29; 'handling': 0.30; "i've": 0.31; 'pretty': 0.32; 'header:User-Agent:1': 0.33; 'there': 0.33; 'to:addr:python- list': 0.34; 'try:': 0.34; 'especially': 0.35; 'subject:How': 0.35; 'conjunction': 0.37; 'but': 0.37; 'except': 0.37; 'list,': 0.37; "there's": 0.37; 'some': 0.38; "it's": 0.40; 'to:addr:python.org': 0.40; 'more': 0.61; 'kind': 0.61; 'below': 0.63; 'special': 0.68; 'suggests.': 0.84 Date: Thu, 15 Dec 2011 12:01:28 -0600 From: Tim Chase User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16 MIME-Version: 1.0 To: Python Subject: Re: How to generate "a, b, c, and d"? References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> In-Reply-To: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> 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 - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Source: X-Source-Args: X-Source-Dir: Cc: Roy Smith X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323972100 news.xs4all.nl 6978 [2001:888:2000:d::a6]:58891 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17290 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" )