views:

80

answers:

5

I know how python dictionaries store key: value tuples. In the project I'm working on, I'm required to store key associated with a value that's a list. ex: key -> [0,2,4,5,8] where, key is a word from text file the list value contains ints that stand for the DocIDs in which the word occurs.

as soon as I find the same word in another doc, i need to append that DocID to the list.

How can I achieve this?

A: 

If i get your question right,You can try this ,

           >>> a=({'a':1,'b':2});
           >>> print a['a']
            1
           >>> a.update({'a':3})
           >>> print a['a']
            3
            >>> a.update({'c':4})
            >>> print a['c']
             4

This will work with older versions of python

Rajeev
-1 You did not get his question right. The OP wants the value to be a list and new numbers to be appended to that list. Your example overwrites old entries with new ones.
Space_C0wb0y
@Space_C0wb0y: The example that i have given says that c is a new value which is added to the dictionary.Old values were just an example to update the values as you said...
Rajeev
@Rajeev In your first line, `a['a'] = 1`. After the next update, `a['a'] = 3`. This means that the old value of `'a'` has been overwritten with a new one. What the OP wants is `a['a'] = [1, 3]`.
Space_C0wb0y
@Space_C0wb0y:As i said earlier it was just an example..
Rajeev
A: 

Something like this?


word = 'something'
l = [0,2,4,5,8]
myDict = {}
myDict[word] = l

#Parse some more

myDict[word].append(DocID)

Falmarri
+1  A: 

This would be a good place to use defaultdict

from collections import defaultdict

docWords = defaultdict(set)
for docID in allTheDocIDs:
    for word in wordsOfDoc(docID):
        docWords[word].add(docID)

you can use a list instead of a set if you have to

cobbal
@cobbal:Default dict was introduced in latest versions of python.i.e, from 2.5,so the code may not work for older versions
Rajeev
I think he means 'defaultdict(set)' not 'defaultDict(set)', but I second this idea.
BenHayden
@Ben I got it right in half the places (fixed now)
cobbal
+3  A: 

You can use defauldict, like this:

>>> import collections
>>> d = collections.defaultdict(list)
>>> d['foo'].append(9)
>>> d
defaultdict(<type 'list'>, {'foo': [9]})
>>> d['foo'].append(90)
>>> d
defaultdict(<type 'list'>, {'foo': [9, 90]})
>>> d['bar'].append(5)
>>> d
defaultdict(<type 'list'>, {'foo': [9, 90], 'bar': [5]})
Vinko Vrsalovic
perfect! this works. So what exactly is collections? is it a new data structure altogether?
csguy11
@csguy11: `collections` is a standard module that provides some data structures.
Roberto Bonvallet
oh ok, got it. Thanks!
csguy11
I actually wrote me a special `listdict` for a project where I use this a lot. It behaves like a `defaultdict(list)`, but the `update`-method appends new values instead of overwriting them.
Space_C0wb0y
A: 

I once wrote a helper class to make @Vinko Vrsalovic`s answer easier to use:

class listdict(defaultdict):
    def __init__(self):
        defaultdict.__init__(self, list)

    def update(self, E=None, **F):
        if not E is None:
            try:
                for k in E.keys():
                    self[k].append(E[k])
            except AttributeError:
                for (k, v) in E:
                    self[k].append(v)
        for k in F:
            self[k].append(F[k])

This can be used like this:

>>> foo = listdict()
>>> foo[1]
[]
>>> foo.update([(1, "a"), (1, "b"), (2, "a")])
>>> foo
defaultdict(<type 'list'>, {1: ['a', 'b'], 2: ['a']})
Space_C0wb0y