我正在将功率(瓦特)值写入文本文件,以便提取可以使用的信息。
4.7
4.7
4.8
5.2
5.1
4.6
4.6
4.6
目前,我有下面的代码来给我平均值,但我想添加到它中,这样它就可以告诉我更多,比如,什么是最高值,最低值,最频繁的值,如果可能的话,如果记录了任何"0"值(*-如果可能,最后一个值最好忽略它们。)
local ctr = 0
local sum = 0
for _ in io.lines"/www/EnergyPlug-b.txt" do
ctr = ctr + 1
end
print(ctr)
for line in io.lines"/www/EnergyPlug-b.txt" do
sum = sum + line
end
print(sum)
average = sum / ctr
print(average)
我确实探索了在第一个io.lines
部分的基础上通过table.insert()
创建Lua表,如下所示,但我不确定它有多好?
local rows = {}
-- go over the file line by line
for line in io.lines("/www/EnergyPlug-b.txt") do
-- if line matches ?.? then insert into table
local value = line:match("%d%p%d") -- 1.5
--print(value)
table.insert(rows, value)
end
local arraymax = math.max(unpack(rows))
local arraymin = math.min(unpack(rows))
print (arraymax) -- ?.?
print (arraymin) -- ?.?
如果以上内容合适,我应该如何最好地识别我一开始提到的项目/价值观?
在第一个片段中,没有理由为ctr和sum单独循环。你可以在一个循环中完成。
你的第二次剪切是可以的。unpack
是有限的,所以这对几千个值都不起作用。
无论如何,你都必须遍历表才能获得其他值,这样你就可以在没有大小限制的情况下确定循环中的最小值和最大值。
local value = line:match("%d%p%d")
如果该文件中只有这些数字,则可以跳过此处的模式匹配。
计算非常精确。我不确定你在这里挣扎着干什么。
local min = math.huge -- use a value that is definitely bigger than any value
local max = -math.huge -- use a value that is definitely smaller than any value
local counts = {} -- an emtpy table we'll use to count how often each value occurs
local numIngored = 0 -- how many 0 values have we ignored?
for line in io.lines(yourFileName) do
-- convert string to a number
local value = tonumber(line)
-- if we take the value into account
if value ~= 0.0 then
-- update min and max values
min = value < min and value or min
max = value > max and value or max
-- update counts
counts[value] = counts[value] and counts[value] + 1 or 1
else
-- count ignored values
numIgnored = numIgnored + 1
end
end
我将让您从counts
中获得最频繁的值
感谢@piglet和使用其他SO帖子,以下是我最终得到的结果。。
local min = math.huge -- use a value that is definitely bigger than any value
local max = -math.huge -- use a value that is definitely smaller than any value
local ctr = 0
local valtotal = 0
local counts = {} -- an emtpy table we'll use to count how often each value occurs
local numIngored = 0 -- how many 0 values have we ignored?
for line in io.lines("/www/EnergyPlug-b.txt") do
-- convert string to a number
local value = tonumber(line)
-- if we take the value into account
if value ~= 0.0 then
ctr = ctr + 1
valtotal = valtotal + value
-- update min and max values
min = value < min and value or min
max = value > max and value or max
-- update counts
counts[value] = counts[value] and counts[value] + 1 or 1
else
-- count ignored values
numIgnored = numIgnored + 1
end
end
print("----Table print out -----")
for k, v in pairs(counts) do
print(k,v)
end
print("---------------------------")
print("Lowest value recorded = " ..min)
print("Highest value recorded = " ..max)
print("Average value recorded = " ..round2(valtotal / ctr, 1))
print("Number of values recorded = " ..ctr)
print("---------------------------")
local max_val, key1 = -math.huge
for k, v in pairs(counts) do
if v > max_val then
max_val, key1 = v, k
end
end
local min_val, key2 = math.huge
for k1, v1 in pairs(counts) do
if v1 < min_val or v1 == min_val then
min_val, key2 = v1, k1
end
end
local min_qty, max_qty = 0, 0
local min_str, max_str = "", ""
for k2, v2 in pairs(counts) do
if v2 == min_val then
min_qty = min_qty + 1
min_str = min_str .. " " .. k2
elseif v2 == max_val then
max_qty = max_qty + 1
max_str = max_str .. " " .. k2
end
end
if min_qty == 1 then
print("Least recorded value was = " ..key2.. " recorded "..min_val.." times")
else
print("Least recorded values were = " ..min_str.. " each recorded "..min_val.." time")
end
if max_qty == 1 then
print("Most recorded value was = " ..key1.. " recorded "..max_val.." times")
else
print("Least recorded values were = " ..max_str.. " each recorded "..max_val.." time")
end
print("---------------------------")