我这里有一个问题...如果我有一个几乎没有重复字符串结果的表。我想知道每个的值(value)。 例如。函数返回未知的“字母”且数量未知
Function () return Table end
Table ={'a','a','c','b','b','a',...}
我想要得到这个。
table.a={'a','a','a'}
table.b={'b','b'}
table.c={'c'}
....
....
我不知道如何解决这个问题...
最佳答案
编写一个函数,创建这些内容的 HashMap :
function RepetitionCounter(tInput)
local tCounter = {}
for i, v in ipairs(tInput) do
tCounter[v] = (tCounter[v] or 0) + 1
end
return tCounter
end
您将按如下方式使用:
local tData = {'a','a','c','b','b','a',...}
local tCounts = RepetitionCounter(tData)
表tCounts
如下所示:
tCounts.a = 3
tCounts.b = 2
tCounts.c = 1
将上面的函数稍微修改一下,就可以得到想要的输出。替换以下行:
tCounter[v] = (tCounter[v] or 0) + 1
与
if not tCounter[v] then
tCounter[v] = {}
else
table.insert(tCounter[v], v)
end
关于variables - 计算表中未知变量的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37720247/