[Zope] Modifying form input which is passed as a record

Casey Duncan cduncan@kaivo.com
Thu, 19 Apr 2001 11:20:47 -0600


Richard Ettema wrote:
> 
> Hi,
> 
> I have a form that passes info as a record with the following type of info:
> 
> [a: 'bob', b: 'silver', a: 'trev', b: 'gold', a: 'nig', b: 'bronze',
> a:'lee', b:'gold']
> 

You cannot have a dictionary (a keyed list) with the same key multiple
times like above. keys MUST be unique!

> I want to work thru this info and pick out all the records with a key 'b'
> equal to 'gold'. I want to place this into a new key-value record (or list?)
> to use later. So it would create:
> 
> [a: 'trev', b: 'gold', a:'lee', b:'gold']
> 
> I cant work out how to do this. My main problem is inserting key names to
> make the key-value pairs.

The value at each key could be a list as in:

{ a: ['trev', 'lee'], b: ['gold', 'gold'] }

A python script could create a dictionary of lists like so:

PARAMETERS: records

r = {}
for rec in records:
   if r.has_key(rec.key):
       r[rec.key].append(rec.value)
   else:
       r[rec.key] = [rec.value]
return r

-- 
| Casey Duncan
| Kaivo, Inc.
| cduncan@kaivo.com
`------------------>