此代码接收网址(nytimes.com),并输出前10个单词出现次数及其出现次数的列表。我得到了前10个单词,但是我的计数是零。有人可以帮我修复计数变量来显示出现次数吗?谢谢!
local http = require("socket.http")
local url = "http://www.nytimes.com"
local body = http.request(url)
local words = {}
for word in string.gmatch(body,"%a+") do
-- print(word)
words[word] = (words[word] or 0) + 1
end
for word, count in pairs(words) do
-- print(words,count)
end
function top1(t)
local max = 0
local maxword
for word, count in pairs(t) do
if count > max then
max = count
maxword = word
end
end
t[maxword] = nil
return maxword, count
end
for i = 1, 10 do
print(top1(words))
end
答案 0 :(得分:1)
您从top1()返回错误的变量 - return maxword, count
应为return maxword, max
。