Search code examples
pythonpython-3.xdata-structuresnltktokenize

Convert Counters to Hash Table with Linked List values


I have 3 counters with total count for word frequency on different strings.

Counter({u'childhood': 3, u'waiting': 2}) #counter1
Counter({u'childhood': 5}) #counter2
Counter({u'waiting': 2}) #counter 3

Atm I am able to perform counter addition to get the total word count for all words in all counters.

Counter({u'childhood': 8, u'waiting': 4})

I however need to take each counter and insert them on a hash table with the word as key and a linked list as value where each linked entry has the count per string per Counter.

Example

[childhood] : [1,3] -> [2,5] #counter 1 - 3 times | counter 2 - 5 times
[waiting] : [1,3] -> [3,2]

How do I achieve this in Python? I was thinking of a dictionary with a deque inside? Or extend the counter addition function?

I'n trying to use existent python data structures without extending or creating custom data structure implementations.


Solution

  • Assuming you have some sequence counters

    total = sum(counters, Counter())
    
    table = {word: [counter[word] for counter in counters] for word in total}
    

    will give you a dictionary like

    {
     'childhood': [3, 5, 0],
     'waiting': [2, 0, 2]
    }