A small proposed change to dictionaries' "get" method

Edward C. Jones edcjones at erols.com
Thu Aug 3 10:41:31 EDT 2000


Gareth McCaughan wrote:

> Consider the following piece of code, which takes a file
> and prepares a concordance saying on which lines each word
> in the file appears. (For real use it would need to be
> made more sophisticated.)
>
>     word2lines = {}
>
>     line_number = 0
>     for line in open(filename).readlines():
>       line_number = line_number+1
>       for word in map(string.lower, string.split(line)):
>         existing_lines = word2lines.get(word, [])   |
>         existing_lines.append(line_number)          | ugh!

I use something like this:

multidict = {}
...
if not multidict.has_key(key):
    multidict[key] = []
multidict[key].append(value)

A multi-dict is similar to the C++ multimap (except hashed). I find them very
useful.

First, the inverse of a dictionary (switch keys and values) is generally a
multi-dict. The inverse of a multi-dict is another multi-dict.

Second, some of the simple concepts behind SQL can be implemented. Suppose
one has a two-dimensional array (a TABLE). It is easy to create a multi-dict
whose keys are the entries in some column of the array (an INDEX). See
<a href="http://members.tripod.com/~edcjones/utility09.html">invert.py</a>
for code.

Keep loving Python,
  Ed Jones





More information about the Python-list mailing list