Python Forum

Full Version: How do you compute tf-idf from a list without using the counter class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a bit of code that works well in calculating term frequency using the counter class import.


from collections import Counter

terms=['the', 'fox', 'the', 'quick', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

tf = Counter(terms)

print(tf)
I am wondering can this be done without the use of the counter class in a simple def.
It can. I have done it but I wouldn't if at that moment I knew for collections.Counter. Big Grin
A hint... See defaultdict
I want to do it without dict
Hm! It's more work but... Just start codding.
(Nov-30-2017, 10:41 PM)syntaxkiller Wrote: [ -> ]I want to do it without dict

in any case use need to end-up with some sort of data structure with term,count pairs - either dict, list/tuple of 2-element list/tuples, etc. and in this case dict is the best option.
This looks liek homework, so I move the thread to the appropriate sub-section
The tuple are immutable type so you have to use list of list pairs [character, counter]
(Dec-01-2017, 09:34 AM)wavic Wrote: [ -> ]The tuple are immutable type so you have to use list of list pairs [character, counter]
That is why I said "end up" - e.g if implement his own counter function, that will iterate over the list (eventually converted to set) and return tuple (term, count) for each term in the input list/set
Sure, I miss my morning coffee.  Smile
(Nov-30-2017, 10:41 PM)syntaxkiller Wrote: [ -> ]I want to do it without dict

Cool.  If you have any trouble, feel free to share what you've tried, and we'll help.

But we won't do it for you.