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


Groups > comp.lang.python > #17322

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!txtfeed1.tudelft.nl!tudelft.nl!txtfeed2.tudelft.nl!amsnews11.chello.com!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'mrab': 0.04; 'received:verizon.net': 0.07; 'terry': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'output': 0.10; 'def': 0.13; '"and': 0.16; "'a'": 0.16; "(i'll": 0.16; 'comma': 0.16; "doesn't.": 0.16; 'handle,': 0.16; 'input,': 0.16; 'reedy': 0.16; 'roy': 0.16; 'settle': 0.16; 'this:': 0.16; 'wrote:': 0.18; 'string,': 0.18; 'jan': 0.19; 'seems': 0.20; 'header:In-Reply- To:1': 0.22; 'slice': 0.23; 'string': 0.24; 'pm,': 0.29; 'handling': 0.30; "i've": 0.31; 'list': 0.32; 'header:User- Agent:1': 0.33; 'header:X-Complaints-To:1': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'copying': 0.35; 'subject:How': 0.35; 'skip:" 10': 0.37; 'two': 0.37; 'but': 0.37; 'list,': 0.37; 'received:org': 0.38; 'some': 0.38; 'created': 0.38; 'to:addr:python.org': 0.40; 'kind': 0.61; 'making': 0.67; 'special': 0.68; '12:27': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: How to generate "a, b, c, and d"?
Date Thu, 15 Dec 2011 19:57:22 -0500
References <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA2DF2.9080107@mrabarnett.plus.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-74-109-121-73.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0
In-Reply-To <4EEA2DF2.9080107@mrabarnett.plus.com>
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.3708.1323997066.27778.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323997066 news.xs4all.nl 6899 [2001:888:2000:d::a6]:41899
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:17322

Show key headers only | View raw


On 12/15/2011 12:27 PM, MRAB wrote:
> On 15/12/2011 16: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.
>
> How about this:
>
> def and_list(items):
> if len(items) <= 2:
> return " and ".join(items)
>
> return ", ".join(items[ : -1]) + ", and " + items[-1]

To avoid making a slice copy,

   last = items.pop()
   return ", ".join(items) + (", and " + last)

I parenthesized the last two small items to avoid copying the long 
string twice with two appends. Even better is

   items[-1] = "and " + items[-1]
   return ", ".join(items)

so the entire output is created in one operation with no copy.

But I would only mutate the list if I started with
   items = list(iterable)
where iterable was the input, so I was mutating a private copy.

-- 
Terry Jan Reedy

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