是否可以计算 Python 中列表中元素出现的相对频率?
例如:
['apple', 'banana', 'apple', 'orange'] # apple for example would be 0.5
请您参考如下方法:
您可以为此使用 NLTK:
import ntlk
text = ['apple', 'banana', 'apple', 'orange']
fd = nltk.FreqDist(text)
查看 tutorial in the book how to和 the source code
或者,您可以使用计数器:
from collections import Counter
text = ['apple', 'banana', 'apple', 'orange']
c = Counter(text)
