【问题标题】:Remove a object from a table listed inside a different table从不同表中列出的表中删除对象
【发布时间】:2021-02-11 02:39:07
【问题描述】:

所以我有两张桌子

local table1 = {1,2,3,4,5,6,7,8,9}
local table2 = {2,4,6,8}

我想从表 1 中删除表 2 中的数字,然后使用在更多代码中删除数字的表一。我该怎么做呢?

【问题讨论】:

  • 删除并创建一个间隙?还是删除并缩小数组?
  • 删除和缩小,但是将两者的差异放在一个新的表idk中可能会更简单
  • 有几种方法可以实现它,复杂性取决于:最小和最大数字是多少?数组排序了吗?可以使用索引表吗?你尝试了什么?
  • 我已经尝试过这些解决方案,尽管我可能做错了。 stackoverflow.com/questions/57388045/… 我需要访问其中包含一堆对象的文件夹,在所有这些对象中,我只需要影响某些对象。脚本也会在文件夹中生成更多对象,因此脚本是间接的。我正在尝试使用某人给我的脚本中的预先完成的数组,所以我不确定它是哪种类型。但是,我自己制作了黑名单对象,尽管我不确定它是否已排序。

标签: lua roblox


【解决方案1】:

当您将数据读入表中时,将您正在读取的任何内容的索引值设置为表中的键。这样可以避免冲突并允许与另一个表进行轻松比较。

local table1 = {} 
local table2 = {}

-- read in your values as keys into the two tables
-- just an example I have no idea how you are populating the tables but hope it helps
table1 = {['input1'] = 0, ['input6'] = 0, ['input3'] = 0}
table2 = {['input1'] = 0, ['input3'] = 0}
newTable = {}

function tableHasKey(table, key)
    return table[key] ~= nil
end

for key, _ in pairs(table1) do
  if not tableHasKey(table2, key) then
    table.insert(newTable, key)
  end 
end

for _, value in pairs(newTable) do
  print(value)
end

当我运行它时,结果是“input6”。这意味着所有不在 table2 中但在 table1 中的值现在都在 newTable 中。如果表之间的值是唯一的,则比较键比比较值更容易。最终结果是一个索引表,结果存储在哈希对象的值中。

【讨论】:

    【解决方案2】:
    #! /usr/bin/env lua
    
    local table1 = {1,2,3,4,5,6,7,8,9}
    local table2 = {2,4,6,8}
    
    for number = 1, #table1 do
        for delete = 1, #table2 do
            if table1[number] == table2[delete] then
                for i = number,  #table1 -1 do
                    table1[i] = table1[i +1]
                end  --  shuffle every entry in table down
                table1 [#table1] = nil  --  erase last entry
            end  --  number == delete
        end  --  loop through table2
    end  --  loop through table1
    
    for i = 1, #table1 do print( table1[i] ) end
    

    1
    3
    5
    7
    9

    【讨论】:

      【解决方案3】:

      此处列出的其他答案似乎是正确的,但它们在内存或时间复杂度方面似乎过于激进。这是我对针对数字列表优化的过滤功能的看法。

      -- t1 : table, the original set of values
      -- t2 : table, the set of values to remove from t1
      -- returns : table, a subset of elements from t1 not found in t2
      local function filter(t1, t2)
      
          -- Assumptions :
          -- 1) t1 and t2 are arrays, not dictionaries
          -- 2) t1 and t2 do not have mixed indices or mixed values
          -- 3) t1 and t2 are sorted
          assert(type(t1) == "table", "t1 expected to be a table")
          assert(type(t2) == "table", "t2 expected to be a table")
          assert(type(next(t1)) == "number" or type(next(t1)) == "nil", "t1 expected to be an array")
          assert(type(next(t2)) == "number" or type(next(t2)) == "nil", "t2 expected to be an array")
      
          -- Early Outs :
          if #t1 == 0 then
              return {}
          elseif #t2 == 0 then
              return t1
          end
      
          -- step through each list and compare each index as you go
          local filteredT = {}
      
          local i = 1
          local j = 1
          local sizeT1 = #t1
          local sizeT2 = #t2
      
          while (i <= sizeT1) and (j <= sizeT2) do
              if t1[i] == t2[j] then
                  -- found a match, exclude from output
                  i = i + 1
                  j = j + 1
      
              elseif t1[i] < t2[j] then
                  -- no match, add elements from t1
                  table.insert(filteredT, t1[i])
                  i = i + 1
      
              else -- t1[i] > t2[j]
                  -- no match, ignore elements from t2
                  j = j + 1
      
              end
          end
      
          -- we've made it to the end of one of the lists, add the rest of t1
          for i = i, sizeT1, 1 do
              table.insert(filteredT, t1[i])
          end
      
          return filteredT
      end
      

      此解决方案没有不必要的循环迭代和许多用于优化的早期输出。

      local a = {1,2,3,4,5,6,7,8,9}
      local b = {2,4,6,8}
      local result1 = filter(a, b)
      print(table.concat(result1, ", ")) -- 1, 3, 5, 7, 9
      
      local c = {"a", "b", "c", "d"}
      local d = {"c", "d"}
      local result2 = filter(c, d)
      print(table.concat(result2, ", ")) -- "a", "b"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-03
        • 2020-04-17
        • 1970-01-01
        • 1970-01-01
        • 2012-04-03
        • 2016-12-19
        相关资源
        最近更新 更多