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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'mrab': 0.04; '>>>>': 0.09; 'from:addr:python': 0.09; 'converting': 0.15; "(i'll": 0.16; 'bit.': 0.16; 'comma': 0.16; 'comma,': 0.16; 'element,': 0.16; 'elements,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'iterator': 0.16; 'iterators,': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'roy': 0.16; 'settle': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'string,': 0.18; 'memory': 0.21; 'header:In-Reply-To:1': 0.22; 'wondered': 0.23; 'string': 0.24; 'code': 0.25; '(in': 0.26; 'bit': 0.28; 'odd': 0.29; 'handling': 0.30; "i've": 0.31; 'source': 0.31; 'cases': 0.32; '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; 'received:84': 0.34; 'too': 0.34; 'file.': 0.34; 'running': 0.35; 'especially': 0.35; 'subject:How': 0.35; 'beginning': 0.36; 'question': 0.36; 'example,': 0.37; 'conjunction': 0.37; 'but': 0.37; 'list,': 0.37; "there's": 0.37; 'cases,': 0.38; 'too,': 0.38; 'some': 0.38; 'why': 0.39; "it's": 0.40; 'to:addr:python.org': 0.40; 'more': 0.61; 'happen': 0.61; 'memory,': 0.67; 'special': 0.68; 'billion': 0.73; 'suggests.': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=J8QoHXbS c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=9jsOeB20M3cA:10 a=HOvpTpyCj2EA:10 a=8nJEP1OIZ-IA:10 a=_XIFnQdksuHyCaUy7v4A:9 a=Ka89JWGbzeLnIFaJLRwA:7 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Thu, 15 Dec 2011 19:27:09 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 To: 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> <4EEA3A47.5020106@stoneleaf.us> <4EEA41C5.60107@tim.thechases.com> In-Reply-To: <4EEA41C5.60107@tim.thechases.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323977203 news.xs4all.nl 6875 [2001:888:2000:d::a6]:45813 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17309 On 15/12/2011 18:51, Tim Chase wrote: > On 12/15/11 12:19, Ethan Furman wrote: >> 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? >>> >>> 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. >> >> 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? > > For the fun/challenge? Because you have a REALLY big data source > that you don't want to keep in memory (in addition the resulting > string)? > > Yeah, for most non-pathological cases, it would make more sense to > just make it a list and then deal with the 4 cases (no elements, one > element, 2 elements, and>2 elements) individually. > I was going to question it too, but then I wondered what would happen if there were a very large number of items and the string would be too big for memory, for example, writing a list of all the numbers from one to a billion to a file.