Posts: 2
Threads: 1
Joined: Nov 2017
Nov-30-2017, 10:30 PM
(This post was last modified: Dec-01-2017, 08:14 AM by buran.)
I have a bit of code that works well in calculating term frequency using the counter class import.
1 2 3 4 5 6 7 |
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.
Posts: 2,953
Threads: 48
Joined: Sep 2016
It can. I have done it but I wouldn't if at that moment I knew for collections.Counter. 
A hint... See defaultdict
Posts: 2
Threads: 1
Joined: Nov 2017
I want to do it without dict
Posts: 2,953
Threads: 48
Joined: Sep 2016
Hm! It's more work but... Just start codding.
Posts: 8,168
Threads: 160
Joined: Sep 2016
Dec-01-2017, 08:19 AM
(This post was last modified: Dec-01-2017, 08:19 AM by buran.)
(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
Posts: 2,953
Threads: 48
Joined: Sep 2016
The tuple are immutable type so you have to use list of list pairs [character, counter]
Posts: 8,168
Threads: 160
Joined: Sep 2016
Dec-01-2017, 09:45 AM
(This post was last modified: Dec-01-2017, 09:49 AM by buran.)
(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
Posts: 2,953
Threads: 48
Joined: Sep 2016
Sure, I miss my morning coffee.
Posts: 3,458
Threads: 101
Joined: Sep 2016
(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.
|