Is there an easy way to output that as: "foo, bar, and twinky"
(There are handy functions for this in PHP, ColdFusion, and Rails, but
my Google-fu fails for Python or Django.)
Thanks,
--
Austin Govella
Thinking & Making: IA, UX, and IxD
http://thinkingandmaking.com
austin....@gmail.com
> I have a list from a queryset:
> * foo
> * bar
> * twinky
>
> Is there an easy way to output that as: "foo, bar, and twinky"
If you want to do it in python, use reduce:
----------------------------------------------------------------------
# l is the list, r is the humanised result
r = reduce(lambda x, y: str(x) + "," + str(y), l)
----------------------------------------------------------------------
or similar.
James
--
/--------------------------------------------------------------------------\
James Aylett xapian.org
ja...@tartarus.org uncertaintydivision.org
http://www.djangoproject.com/documentation/templates/#join
{{ your_list|join:", " }}
Jonathan.
>> Is there an easy way to output that as: "foo, bar, and twinky"
>
> If you want to do it in python, use reduce:
>
> ----------------------------------------------------------------------
> # l is the list, r is the humanised result
> r = reduce(lambda x, y: str(x) + "," + str(y), l)
> ----------------------------------------------------------------------
or more efficiently,
text = ", ".join(map(str, data))
but this only inserts commas. maybe you could add something like this
to a suitable templatetags module:
def join_en(seq):
seq = map(str, seq)
if not seq:
return "" # empty
if len(seq) == 1:
return seq[0]
elif len(seq) == 2:
return " and ".join(seq)
text = ", ".join(seq[:-1])
return text + ", and " + seq[-1]
register.filter(join_en)
tweak as necessary.
(note that there is a "humanize" template tag collection under contrib,
but that one is currently focussed on numbers, not lists).
</F>
Not that we need another way but this would work as well, and it
doesn't need special cases for one and two item lists but does insert
the "and" (unlike most of the solutions offered):
def humanize_list(list):
return ", and".join(map(str, ", ".join(map(str, list)).rsplit(',',1)))
--
----
Waylan Limberg
way...@gmail.com
> Not that we need another way but this would work as well, and it
> doesn't need special cases for one and two item lists but does insert
> the "and" (unlike most of the solutions offered):
>
> def humanize_list(list):
> return ", and".join(map(str, ", ".join(map(str, list)).rsplit(',',1)))
>>> humanize_list(["one", "two"])
'one, and two'
</F>
> from django.utils.text import get_text_list
doesn't match the OP's specification for all lists, though:
>>> get_text_list(["one", "two", "three"], 'and')
'one, two and three'
but I guess you could always do
if len(seq) >= 2:
text = get_text_list(seq, ", and")
else:
text = get_text_list(seq, "and")
</F>
As far as template logic is concerned, I do something similar on my
blog to list the categories each post is in; I just use the {% for %}
tag to loop over the list of categories, and added a couple checks to
determine whether to put a comma after each one and where to put the
'and' (using tests for 'forloop.last' within the loop).
--
"May the forces of evil become confused on the way to your house."
-- George Carlin