在Lua列表中search一个项目

如果我有这样的项目列表:

local items = { "apple", "orange", "pear", "banana" } 

如何检查“橙色”是否在这个列表中?

在Python中,我可以这样做:

 if "orange" in items: # do something 

Lua有没有相同的function?

你可以用Lua中的Programming来设置一些东西:

 function Set (list) local set = {} for _, l in ipairs(list) do set[l] = true end return set end 

然后你可以把你的列表放在设置和testing成员资格:

 local items = Set { "apple", "orange", "pear", "banana" } if items["orange"] then -- do something end 

或者你可以直接遍历列表:

 local items = { "apple", "orange", "pear", "banana" } for _,v in pairs(items) do if v == "orange" then -- do something break end end 

使用下面的表示代替:

 local items = { apple=true, orange=true, pear=true, banana=true } if items.apple then ... end 

你只能看到Lua只有一个数据结构的缺点 – 你必须自己动手。 如果你坚持使用Lua,你将逐渐积累一个函数库,以你喜欢的方式操作表格。 我的图书馆包括一个列表到设置的转换和一个高级列表searchfunction:

 function table.set(t) -- set of list local u = { } for _, v in ipairs(t) do u[v] = true end return u end function table.find(f, l) -- find element v of l satisfying f(v) for _, v in ipairs(l) do if f(v) then return v end end return nil end 

Lua表格更类似于Python词典,而不是列表。 您创build的表基本上是一个基于1的索引string数组。 使用任何标准的searchalgorithm来确定一个值是否在数组中。 另一种方法是将值存储为表键,而不是像Jon Ericson的post的设置实现中所示的那样。

 function valid(data, array) local valid = {} for i = 1, #array do valid[array[i]] = true end if valid[data] then return false else return true end end 

这里是我用来检查数据是否在数组中的函数。

使用metatable的解决schemesorting…

 local function preparetable(t) setmetatable(t,{__newindex=function(self,k,v) rawset(self,v,true) end}) end local workingtable={} preparetable(workingtable) table.insert(workingtable,123) table.insert(workingtable,456) if workingtable[456] then ... end 

这是您可以使用的瑞士军刀function:

 function table.find(t, val, recursive, metatables, keys, returnBool) if (type(t) ~= "table") then return nil end local checked = {} local _findInTable local _checkValue _checkValue = function(v) if (not checked[v]) then if (v == val) then return v end if (recursive and type(v) == "table") then local r = _findInTable(v) if (r ~= nil) then return r end end if (metatables) then local r = _checkValue(getmetatable(v)) if (r ~= nil) then return r end end checked[v] = true end return nil end _findInTable = function(t) for k,v in pairs(t) do local r = _checkValue(t, v) if (r ~= nil) then return r end if (keys) then r = _checkValue(t, k) if (r ~= nil) then return r end end end return nil end local r = _findInTable(t) if (returnBool) then return r ~= nil end return r end 

你可以用它来检查一个值是否存在:

 local myFruit = "apple" if (table.find({"apple", "pear", "berry"}, myFruit)) then print(table.find({"apple", "pear", "berry"}, myFruit)) -- 1 

您可以使用它来查找关键字:

 local fruits = { apple = {color="red"}, pear = {color="green"}, } local myFruit = fruits.apple local fruitName = table.find(fruits, myFruit) print(fruitName) -- "apple" 

我希望recursive参数能够说明问题。

metatables参数还允许您searchmetatables。

keys参数使函数查找列表中的键。 当然这在Lua中是没有用的(你可以做fruits[key] ),但是和recursivemetatables ,它变得方便。

returnBool参数对于在表中具有false作为键时是安全的(是的,这是可能的: fruits = {false="apple"}

Interesting Posts