[Zope] eliminating dupes in a list
Tres Seaver
tseaver@palladion.com
Tue, 04 Apr 2000 14:41:56 -0500
sathya <linuxcraft@redspice.com> asked:
> I have a list to pass in as a parameter to dtml-in but before doing that I
> would like to eliminate duplicates form the list.
> ie in ['1','2','1'] I want to skip the duplicate 1. is there a zope hack for
> this or do I have to use an external method
This requires some Python expression trickery which can't (currently) be done
within DTML (filter and map aren't available to DTML). You are probably better
off using a PythonMethod for such logic. For grins, I used the Python
interpreter to bang out the following Python expression:
filter( None, map( lambda i, d={}:
( i, None )[ d.has_key(i) or d.update( {i: 1} ) or 0 ]
, foo ) )
This is too convoluted to use in production code (and it strips out 0 values,
too) -- much better a nice, straightforward, "Pythonic" solution, a Python
method 'uniq' taking a single argument, 'items':
d = {}
for item in items:
if not d.has_key( item ):
d.update( { item: 1 } )
return d.keys()
Call from DTML:
<dtml-in "uniq( myItems )" sort>
...
</dtml-in>
--
=========================================================
Tres Seaver tseaver@digicool.com tseaver@palladion.com