473,378 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Re: formatting list -> comma separated (slightly different)

Paul & Robert wrote...
d = ["soep", "reeds", "ook"]
print ', '.join(d)
soep, reeds, ook
I occasionally have a need for printing lists of items too, but in the form:
"Butter, Cheese, Nuts and Bolts". The last separator is the word 'and'
instead of the comma. The clearest I could come up with in Python is below.
I wonder if there is a more pythonic solution for this problem. Maybe
something recursive?

Greetings,
'''
Formatting a sequence of items such that they are separated by
commas, except the last item, which is separated by the word 'and'.

Used for making lists of dates and items more human-readable
in generated emails and webpages.

For example:

Four friends have a dinner: Anne, Bob, Chris and Debbie
Three friends have a dinner: Anne, Bob and Chris
Two friends have a dinner: Anne and Bob
One friend has a dinner: Anne
No friend has a dinner:

['Anne','Bob','Chris','Debbie'] -"Anne, Bob, Chris and Debbie"
['Bob','Chris','Debbie'] -"Bob, Chris and Debbie"
['Chris','Debbie'] -"Chris and Debbie"
['Debbie'] -"Debbie"
[] -""

'''

def pretty(f):
if len(f)==0: return ''
if len(f)==1: return f[0]
sepwithcommas=f[:-1]
sepwithand=f[-1]
s=', '.join(sepwithcommas)
if sepwithand:
s+=' and '+sepwithand
return s

friends=['Anne','Bob','Chris','Debbie','Eve','Fred']
while True:
print friends,'->',pretty(friends)
if friends:
friends.pop(0)
else:
break

--
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Vallopillil
http://www.catb.org/~esr/halloween/halloween4.html

Jul 9 '08 #1
3 1568
Michiel Overtoom wrote:
Paul & Robert wrote...
>d = ["soep", "reeds", "ook"]
print ', '.join(d)
soep, reeds, ook

I occasionally have a need for printing lists of items too, but in the form:
"Butter, Cheese, Nuts and Bolts". The last separator is the word 'and'
instead of the comma. The clearest I could come up with in Python is below.
I wonder if there is a more pythonic solution for this problem. Maybe
something recursive?

Greetings,
'''
Formatting a sequence of items such that they are separated by
commas, except the last item, which is separated by the word 'and'.

Used for making lists of dates and items more human-readable
in generated emails and webpages.

For example:

Four friends have a dinner: Anne, Bob, Chris and Debbie
Three friends have a dinner: Anne, Bob and Chris
Two friends have a dinner: Anne and Bob
One friend has a dinner: Anne
No friend has a dinner:

['Anne','Bob','Chris','Debbie'] -"Anne, Bob, Chris and Debbie"
['Bob','Chris','Debbie'] -"Bob, Chris and Debbie"
['Chris','Debbie'] -"Chris and Debbie"
['Debbie'] -"Debbie"
[] -""

'''

def pretty(f):
if len(f)==0: return ''
if len(f)==1: return f[0]
sepwithcommas=f[:-1]
sepwithand=f[-1]
s=', '.join(sepwithcommas)
if sepwithand:
s+=' and '+sepwithand
return s

friends=['Anne','Bob','Chris','Debbie','Eve','Fred']
while True:
print friends,'->',pretty(friends)
if friends:
friends.pop(0)
else:
break


IMHO slightly easier to read and faster if the lists are long:

def pretty(f):
if len(f) == 0 :
return ""
elif len(f) == 1:
return f[0]
else:
return ', '.join(f[:-1]) + ' and ' + f[-1]
-Larry
Jul 9 '08 #2
Michiel Overtoom <mo****@xs4all.nlwrote:
I occasionally have a need for printing lists of items too, but in the
form: "Butter, Cheese, Nuts and Bolts". The last separator is the
word 'and' instead of the comma. The clearest I could come up with in
Python is below. I wonder if there is a more pythonic solution for
this problem. Maybe something recursive?
>>from itertools import chain, repeat
def across(*iterables):
iterables = map(iter, iterables)
while 1:
for it in iterables:
yield it.next()

>>friends=['Anne','Bob','Chris','Debbie','Eve','Fred']
''.join(across(friends, chain(repeat(', ', len(friends)-2), [' and
'])))
'Anne, Bob, Chris, Debbie, Eve and Fred'

I feel there ought to be an easier way of alternating between iterators but
if there is I can't think of it offhand. Also it breaks down unless you
have at least two friends.

This version is a bit messy but seems to work reliably for any number of
friends:
>>for i in range(len(friends)+1):
f = friends[:i]
print ' and '.join(s for s in [', '.join(f[:-1]), ''.join(f[-1:])] if
s)

Anne
Anne and Bob
Anne, Bob and Chris
Anne, Bob, Chris and Debbie
Anne, Bob, Chris, Debbie and Eve
Anne, Bob, Chris, Debbie, Eve and Fred

Of course neither works cleanly if you give it an iterator rather than a
list. This is a bit longer but does work without knowing the length of the
iterator in advance (and you could save a couple of lines by concatenating
the adjacent yields):
>>def commaand(it):
it = iter(it)
yield it.next()
n = it.next()
for nxt in it:
yield ', '
yield n
n = nxt
yield ' and '
yield n

>>for i in range(len(friends)+1):
f = friends[:i]
print ''.join(commaand(f))

Anne
Anne and Bob
Anne, Bob and Chris
Anne, Bob, Chris and Debbie
Anne, Bob, Chris, Debbie and Eve
Anne, Bob, Chris, Debbie, Eve and Fred
Jul 9 '08 #3
On 9 Juli, 22:25, Michiel Overtoom <mot...@xs4all.nlwrote:
Paul & Robert wrote...
d = ["soep", "reeds", "ook"]
print ', '.join(d)
soep, reeds, ook

I occasionally have a need for printing lists of items too, but in the form:
"Butter, Cheese, Nuts and Bolts". *The last separator is the word 'and'
instead of the comma. The clearest I could come up with in Python is below.
I wonder if there is a more pythonic solution for this problem. *Maybe
something recursive?
[snip]
def pretty(f):
* * if len(f)==0: return ''
* * if len(f)==1: return f[0]
* * sepwithcommas=f[:-1]
* * sepwithand=f[-1]
* * s=', '.join(sepwithcommas)
* * if sepwithand:
* * * * s+=' and '+sepwithand
* * return s
def pretty(names):
return ' and '.join(', '.join(names).rsplit(', ', 1))

friends=['Anne','Bob','Chris','Debbie','Eve','Fred']
print pretty(friends)
Jul 22 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Timo | last post by:
I have this list of checkboxes displaying properly in Opera and IE, but cannot get it in Firefox. For some reason, in FF, the checkboxes creep farther and farther to the right across the page,...
3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
2
by: Sara | last post by:
The problem: Conditional formatting bold, red when field Value < date() sets the field background to white - always - whether condition is met or not. I want the field unfilled and just red/bold...
4
by: deko | last post by:
I've heard it's best not to have any formatting specified for Table fields (except perhaps Currency), and instead set the formatting in the Form or Report. But what about Yes/No fields? When I...
0
by: George Durzi | last post by:
I have a stored procedure that is returning a column which is cast(company_name as char(30)) + ' | ' + cast(client_name as char(20) + ' | ' 2by4 Brand Architects | Test, TEst ...
2
by: jodyblau | last post by:
I'm not certain that what I am trying to do is possible; in any event I haven't been able to figure it out. Here is what I am trying to do: I have one table that has a list of cases I'm working...
1
by: fatima.issawi | last post by:
Hello, I am creating a web page using c# and asp.net. I am trying to populate a list box by reading text from a file. The text in the file has leading spaces in order to show a hierarchy of...
4
by: slinky | last post by:
Thanks in advance... I have a continuous style form with a field for each record called "STATUS". I simply want to have the form load and if the value of the textbox is "Inactive" I want the...
2
by: Tim Chase | last post by:
Is there an easy way to make string-formatting smart enough to gracefully handle iterators/generators? E.g. transform = lambda s: s.upper() pair = ('hello', 'world') print "%s, %s" % pair #...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.