在Lua列表中搜索项目

时间:2009-03-17 21:56:38

标签: list lua lua-table

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

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

如何检查此列表中是否有“橙色”?

在Python中,我可以做到:

if "orange" in items:
    # do something

Lua中有等同物吗?

11 个答案:

答案 0 :(得分:71)

您可以使用Programming in Lua中的集合:

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

然后,您可以将您的列表放入Set并测试成员身份:

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

答案 1 :(得分:25)

请改用以下表示法:

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

答案 2 :(得分:17)

你是第一手看到Lua只有一种数据结构的缺点 - 你必须自己动手。如果你坚持使用Lua,你将逐渐积累一个函数库,以你喜欢的方式操作表。我的库包括列表到集的转换和高阶列表搜索功能:

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

答案 3 :(得分:3)

Lua表更接近于Python词典而非列表。您创建的表基本上是一个基于1的索引字符串数组。使用任何标准搜索算法来确定值是否在数组中。另一种方法是将值存储为表键,而不是如Jon Ericson的帖子的set实现中所示。

答案 4 :(得分:2)

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

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

答案 5 :(得分:1)

使用metatable的解决方案......

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

答案 6 :(得分:1)

这是你可以使用的瑞士军刀功能:

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参数也允许您搜索元表。

keys参数使函数查找列表中的键。当然,这在Lua中是无用的(你可以fruits[key]),但与recursivemetatables一起,它会变得很方便。

returnBool参数是一种安全保护措施,用于表格中false作为表中的键(是的,可能:fruits = {false="apple"}

答案 7 :(得分:1)

可以使用一个简单的函数:

  • 返回 nil,如果在表中找不到该项目
  • 如果在表中找到项目,则返回项目的索引
local items = { "apple", "orange", "pear", "banana" }

local function search_value (tbl, val)
    for i = 1, #tbl do
        if tbl[i] == val then
            return i
        end
    end
    return nil
end

print(search_value(items, "pear"))
print(search_value(items, "cherry"))

上面代码的输出是

3
nil

答案 8 :(得分:0)

function table.find(t,value)
    if t and type(t)=="table" and value then
        for _, v in ipairs (t) do
            if v == value then
                return true;
            end
        end
        return false;
    end
    return false;
end

答案 9 :(得分:0)

您可以使用以下解决方案:

items = { 'a', 'b' }
for k,v in pairs(items) do 
 if v == 'a' then 
  --do something
 else 
  --do something
 end
end

items = {'a', 'b'}
for k,v in pairs(items) do 
  while v do
    if v == 'a' then 
      return found
    else 
      break
    end
  end 
 end 
return nothing

答案 10 :(得分:0)

随意编写它,但是直接在列表上迭代 比生成pair()或ipairs()更快。

#! /usr/bin/env lua

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

local function locate( table, value )
    for i = 1, #table do
        if table[i] == value then print( value ..' found' ) return true end
    end
    print( value ..' not found' ) return false
end

locate( items, 'orange' )
locate( items, 'car' )

发现橙色
找不到车

相关问题