频率分布Python。。。最后一期

频率分布Python。。。最后一期,python,Python,当我尝试打印FreqDist对象时,在打印结束时会得到“…”? 我试着在网上找,但找不到 请告诉我哪里出了问题 代码: for word in nltk.word_tokenize(lin): fdist.inc(word) print fdist 使用fdist时,它返回键值对列表。你必须用循环打印出来。下面这样的方法应该可以工作: import nltk from nltk.tokenize import word_tokenize lin = "A frequency dis

当我尝试打印FreqDist对象时,在打印结束时会得到“…”? 我试着在网上找,但找不到

请告诉我哪里出了问题

代码:

for word in nltk.word_tokenize(lin):
    fdist.inc(word)

print fdist

使用
fdist
时,它返回键值对列表。你必须用循环打印出来。下面这样的方法应该可以工作:

import nltk
from nltk.tokenize import word_tokenize

lin = "A frequency distribution for the outcomes of an experiment. A frequency distribution records the number of times each outcome of an experiment has occurred. For example, a frequency distribution could be used to record the frequency of each word type in a document. Formally, a frequency distribution can be defined as a function mapping from each sample to the number of times that sample occurred as an outcome."

fdist = nltk.FreqDist()

for word in word_tokenize(lin):
    fdist.inc(word)

for f in fdist:
    print f, fdist[f]
结果是:

frequency 5
of 5
a 4
distribution 4
the 4
an 3
each 3
, 2
A 2
as 2
be 2
number 2
outcome 2
sample 2
times 2
to 2
. 1
For 1
Formally 1
can 1
could 1
defined 1
document. 1
example 1
experiment 1
experiment. 1
for 1
from 1
function 1
has 1
in 1
mapping 1
occurred 1
occurred. 1
outcomes 1
record 1
records 1
that 1
type 1
used 1
word 1
[Finished in 1.5s]
让我们知道这是否有帮助

编辑:

另一种方法:

import nltk
from nltk.tokenize import word_tokenize

lin = "A frequency distribution for the outcomes of an experiment. A frequency distribution records the number of times each outcome of an experiment has occurred. For example, a frequency distribution could be used to record the frequency of each word type in a document. Formally, a frequency distribution can be defined as a function mapping from each sample to the number of times that sample occurred as an outcome."

tokens = word_tokenize(lin)
fdist = nltk.FreqDist(tokens)

for f in fdist:
    print f, fdist[f]

输出是相同的。

请单击此帖子顶部附近的复选标记,将答案标记为已接受。这就是我们在SO中说谢谢的方式。