Pre-PEP: Dictionary accumulator methods
Jeff Epler
jepler at unpythonic.net
Sat Mar 19 09:00:48 EST 2005
> [Jeff Epler]
> > Maybe something for sets like 'appendlist' ('unionset'?)
>
On Sat, Mar 19, 2005 at 04:18:43AM +0000, Raymond Hettinger wrote:
> I do not follow. Can you provide a pure python equivalent?
Here's what I had in mind:
$ python /tmp/unionset.py
Set(['set', 'self', 'since', 's', 'sys', 'source', 'S', 'Set', 'sets', 'starting'])
#------------------------------------------------------------------------
try:
set
except:
from sets import Set as set
def unionset(self, key, *values):
try:
self[key].update(values)
except KeyError:
self[key] = set(values)
if __name__ == '__main__':
import sys, re
index = {}
# We need a source of words. This file will do.
corpus = open(sys.argv[0]).read()
words = re.findall('\w+', corpus)
# Create an index of the words according to the first letter.
# repeated words are listed once since the values are sets
for word in words:
unionset(index, word[0].lower(), word)
# Display the words starting with 'S'
print index['s']
#------------------------------------------------------------------------
Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050319/ff53e345/attachment.sig>
More information about the Python-list
mailing list