简体   繁体   中英

Python: Storing a list value associated with a key in dictionary

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?

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]})

This post was helpful for me to solve a problem I had in dynamically creating variable keys with lists of data attached. See below:

import collections

d = collections.defaultdict(list)
b = collections.defaultdict(list)
data_tables = ['nodule_data_4mm_or_less_counts','nodule_data_4to6mm_counts','nodule_data_6to8mm_counts','nodule_data_8mm_or_greater_counts']

for i in data_tables:
    data_graph = con.execute("""SELECT ACC_Count, COUNT(Accession) AS count
                                            FROM %s
                                            GROUP BY ACC_Count"""%i)
    rows = data_graph.fetchall()
    for row in rows:
        d[i].append(row[0])
        b[i].append(row[1])

print d['nodule_data_4mm_or_less_counts']
print b['nodule_data_4mm_or_less_counts']

Which outputs the data lists for each key and then can be changed to a np.array for plotting etc.

>>>[4201, 1052, 418, 196, 108, 46, 23, 12, 11, 8, 7, 2, 1]
>>>[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]

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

Something like this?


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

#Parse some more

myDict[word].append(DocID)

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']})

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM