【问题标题】:beginner lua: check array for value初学者lua:检查数组的值
【发布时间】:2021-02-25 00:51:10
【问题描述】:

我需要生成一个随机数,然后检查该数字作为值列在哪些表(数组)中。检查完成后,我需要输出出现数字的表格。

例如,将生成 1 到 21 之间的随机数,然后在其他数字表中进行搜索。

evens = {2,4,6,8,10,12,14,16,18,20}
odds = {1,3,5,7,9,11,13,15,17,19,21}
low = {1,2,3,4,5,6,7}
med = {8,9,10,11,12,13,14}
high = {15,16,17,18,19,20,21}

如果 17 是随机数,我需要输出“odds”和“high”。

【问题讨论】:

  • 为了让事情更简单一些,我正在尝试创建一个“if”语句来检查每个表中是否存在数字,然后执行其他一些函数。

标签: lua


【解决方案1】:

我能提供的最通用的解决方案是构建类似inverted index 的东西。您只需要在表中为每个可能的术语(在您的情况下为数字)创建一条记录。此类表中的值表示您可以在其中找到您的术语的数组。 代码可能如下所示:

local evens = { 2, 4, 6, 8, name = 'evens'}
local odds = {1, 3, 5, 7, name = 'odds'}
local low = { 1, 2, 3, 4, name = 'low'}
local high = {15, 16, 17, 18, 19, name = 'high'}

local inv_index = {}

function add_to_index(index, numbers)
    for i, number in ipairs(numbers) do
            local indexed = index[number]
            if not indexed then
                    index[number] = { numbers }
            else
                    table.insert(indexed, numbers)
            end
    end
end

add_to_index(inv_index, evens)
add_to_index(inv_index, odds)
add_to_index(inv_index, low)
add_to_index(inv_index, high)

-- which arrays contains the number "4"?
for k, indexed in pairs(inv_index[4]) do
    print(indexed.name) -- prints "evens" and "low"
end

此解决方案的缺点是内存消耗,尤其是在可能的数字数量很大的情况下。另一种方法是对每个数组进行排序并对其执行binary search。 lua 有一个implementation

如果您可以修改数组,则可以将数字存储在某种集合中:

local evens = { [2] = true, [4] = true, [6] = true, [8] = true }
local low = { [1] = true, [2] = true, [3] = true, [4] = true }
local odds = { [1] = true, [3] = true , [5] = true, [7] = true }

local x = 4

print(evens[x] ~= nil) -- true
print(low[x] ~= nil) -- true
print(odds[x] ~= nil) -- false

【讨论】:

    【解决方案2】:

    不需要检查奇数表,进入范围更容易检查限制:

    local low = {1,2,3,4,5,6,7}
    local med = {8,9,10,11,12,13,14}
    local high = {15,16,17,18,19,20,21}
    
    local n = 33
    local function CheckNum(n)
        local tab_type = 'unknown'
        if n  >= 1 and n <=7 then tab_type = "low"
        elseif n  >= 8 and n <=14  then tab_type = "med"
        elseif n  >= 15 and n <=21 then tab_type = "high"   
        end
        local odd = (n%2==0) and "even" or "odd" 
        return odd,  tab_type
    end
    
    local odd,  tab_type = CheckNum(n)
    print ( odd, " ", tab_type )
    

    【讨论】:

    • 感谢您的回答,尽管我担心我可能有点过于简化了表格。我将使用的许多表格都包含不连续的数字;所以用大于和小于运算符排列它们并不是一个真正的选择。即:{1,4,6,8,9,14,17,18,21} 和 {2,3,5,7,10,11,12,13,15,16,19,20}。如果不手动遍历整个表,是否无法判断一个值是否在表中?
    【解决方案3】:

    我找到了一个检查列表中值的函数:

    function contains(list, x)
        for _, v in pairs(list) do
            if v == x then return true end
        end
        return false
    end
    

    这是一个如何使用它的示例:

    alist={'abc',123}
    if contains(alist,'abc')
        print('abc is in the list alist')
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2015-12-31
      • 2014-06-27
      • 2017-03-26
      相关资源
      最近更新 更多