Is it possible to calculate the relative frequency of elements occurring in a list in Python?
For example:
['apple', 'banana', 'apple', 'orange'] # apple for example would be 0.5
You can use NLTK for this:
import ntlk
text = ['apple', 'banana', 'apple', 'orange']
fd = nltk.FreqDist(text)
Check out the tutorial in the book the how to and the source code
Alternately, you could use a Counter:
from collections import Counter
text = ['apple', 'banana', 'apple', 'orange']
c = Counter(text)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With