[Zope] removing items from lists ?

Eric Walstad eric@walstads.net
Tue, 19 Jun 2001 09:38:20 -0700


Hi Sven,
First off, I would try doing this in something other than a DTML method; a
Python Script or External Method are better choices for logic.
Here's a simple example using a Python Script:

## Script (Python) "randItems"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
# We'll need to use Python's whrandom module
import whrandom

# Your list of choices.  We want 3 of these chosen at random
bozo=['a','b','c','d','e','f','g','h','i','j']

# A list to hold the random selections
RandomSelections = []

# Pick 3 random selections
for i in range(3):
    # Grab a copy of the random selection
    RandomItem = whrandom.choice(bozo)

    # Remove the object from our list of choices
    bozo.remove(RandomItem)

    # Add the item to our list of random selections
    RandomSelections.append(RandomItem)

# Return the list of random selections
return RandomSelections


Hope that helps.

Eric.

> -----Original Message-----
> From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Junk
> Sent: Tuesday, June 19, 2001 3:03 AM
> To: zope@zope.org
> Subject: [Zope] removing items from lists ?
>
>
> hello,
>
> i am making a dtml script that will choose 3 different objects and display
> them and i'm kindda stuck.
>
> i think the process would be :
>
> 1] make a list of objects (dtml-let ?)
> 2] choose a random item from the list (or maybe choose all three
> at one time
> ?)
> 3] remove this item from the list
> 4] same player shoots again...
>
> right now i'm able to make the list, to use whrandom.choice(xx) to get a
> random value, but i can't remove items from the list and once in a while i
> do get the same item twice (which i don't want happening).
>
> maybe i don't do it the right way ;-)
>
> $ven
>
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )