我想找到文本文件中所有单词的频率,以便我可以从中找出最常出现的单词。有人可以帮助我使用为此目的的命令吗?
import nltk
text1 = "hello he heloo hello hi " // example text
fdist1 = FreqDist(text1)
Run Code Online (Sandbox Code Playgroud)
我已经使用了上面的代码,但问题是它没有给出词频,而是显示每个字符的频率。我还想知道如何使用文本文件输入文本。
我在python 2.7中编写了这段代码来查找Fibonnaci系列.但是我的代码中有错误:
File "Fib.py", line 2, in <module>
class Fib:
File "Fib.py", line 21, in Fib
for n in Fib(4):
NameError: name 'Fib' is not defined
Run Code Online (Sandbox Code Playgroud)
谁能解决这个bug?
class Fib:
def __init__(self,max):
self.max = max
def __iter__(self):
self.a=0
self.b = 1
return self
def __next__(self) :
fib = self.a
if fib > self.max :
raise StopIteration
a,b=b,self.a+self.b
return fib
for n in Fib(4):
print n
Run Code Online (Sandbox Code Playgroud) 我为冒泡排序编写了这段代码,它显示了这个错误:
File "bubble.py", line 6
for i in range(0,n)
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我如何使用该range功能,以便我可以n从用户获取输入并循环它?
print "Enter n"
n = raw_input()
print "enter elements"
a = []
for i in range(0,n)
temp = raw_input()
a[i].append(temp)
for i in range(0,n)
for j in range(0,n-1)
if a[j]>a[j+1]
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
for i in range(0,n)
print a[i]
Run Code Online (Sandbox Code Playgroud)