How to generate a, b, c, and d?

2011-12-15 Thread Roy Smith
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

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

2011-12-15 Thread MRAB
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',

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

2011-12-15 Thread Tim Chase
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',

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

2011-12-15 Thread Ethan Furman
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

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

2011-12-15 Thread Tim Chase
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

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

2011-12-15 Thread Roy Smith
FWIW, I ended up with: n = len(names) if n == 0: return '' if n == 1: return names[0] pre = ', '.join(names[:-1]) post = names[-1] return '%s, and %s' (pre, post) the slice-and-join() takes care of both the 2 and 2 element

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

2011-12-15 Thread MRAB
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,

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

2011-12-15 Thread Ian Kelly
On Thu, Dec 15, 2011 at 11:51 AM, Tim Chase python.l...@tim.thechases.com wrote: 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)? If you have that much data, then I question why you would want to build

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

2011-12-15 Thread Terry Reedy
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'

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

2011-12-15 Thread Chris Angelico
On Fri, Dec 16, 2011 at 11:57 AM, Terry Reedy tjre...@udel.edu wrote:  items[-1] = and + items[-1]  return , .join(items) This works only if you're sure there are at least two items, and if you don't mind two items coming out as a, and b. ChrisA --

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

2011-12-15 Thread Terry Reedy
On 12/15/2011 9:42 PM, Chris Angelico wrote: On Fri, Dec 16, 2011 at 11:57 AM, Terry Reedytjre...@udel.edu wrote: items[-1] = and + items[-1] return , .join(items) This works only if you're sure there are at least two items, and if you don't mind two items coming out as a, and b.