在 Lua 列表中搜索项目
如果我有一个这样的项目列表:
local items = { "apple", "orange", "pear", "banana" }
如何检查“橙色”是否在此列表中?
在Python中我可以这样做:
if "orange" in items:
# do something
Lua中有等价的吗?
If I have a list of items like this:
local items = { "apple", "orange", "pear", "banana" }
how do I check if "orange" is in this list?
In Python I could do:
if "orange" in items:
# do something
Is there an equivalent in Lua?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您可以使用 Programming in Lua 中的类似集合:
然后您可以将列表放入设置并测试成员资格:
或者您可以直接迭代列表:
You could use something like a set from Programming in Lua:
Then you could put your list in the Set and test for membership:
Or you could iterate over the list directly:
请改用以下表示形式:
Use the following representation instead:
您将亲眼目睹 Lua 只有一种数据结构的缺点之一——您必须推出自己的数据结构。 如果您坚持使用 Lua,您将逐渐积累一个以您喜欢的方式操作表格的函数库。 我的库包括列表到集合的转换和高阶列表搜索功能:
You're seeing firsthand one of the cons of Lua having only one data structure---you have to roll your own. If you stick with Lua you will gradually accumulate a library of functions that manipulate tables in the way you like to do things. My library includes a list-to-set conversion and a higher-order list-searching function:
你想怎么写就怎么写,但是直接在列表上迭代比生成pairs()或ipairs()要快
找不到车
Write it however you want, but it's faster to iterate directly over the list, than to generate pairs() or ipairs()
orange found
car not found
Lua 表更类似于 Python 字典而不是列表。 您创建的表本质上是一个从 1 开始的索引字符串数组。 使用任何标准搜索算法来查找某个值是否在数组中。 另一种方法是将值存储为表键,而不是如 Jon Ericson 帖子的 set 实现中所示。
Lua tables are more closely analogs of Python dictionaries rather than lists. The table you have create is essentially a 1-based indexed array of strings. Use any standard search algorithm to find out if a value is in the array. Another approach would be to store the values as table keys instead as shown in the set implementation of Jon Ericson's post.
这是一个 swiss-armyknife 函数,您可以使用它:
您可以使用它来检查值是否存在:
您可以使用它来查找密钥:
我希望
recursive
参数不言自明。metatables
参数还允许您搜索元表。keys
参数使函数在列表中查找键。 当然,这在 Lua 中是没有用的(你可以只做fruits[key]
),但是与recursive
和metatables
一起使用,它变得很方便。当您的表中的键为 false 时,
returnBool
参数是一个安全保障(是的,这是可能的:fruits = {false="苹果”}
)This is a swiss-armyknife function you can use:
You can use it to check if a value exists:
You can use it to find the key:
I hope the
recursive
parameter speaks for itself.The
metatables
parameter allows you to search metatables as well.The
keys
parameter makes the function look for keys in the list. Of course that would be useless in Lua (you can just dofruits[key]
) but together withrecursive
andmetatables
, it becomes handy.The
returnBool
parameter is a safe-guard for when you have tables that havefalse
as a key in a table (Yes that's possible:fruits = {false="apple"}
)这是我用来检查数据是否在数组中的函数。
Here's the function I use for checking if data is in an array.
使用元表的解决方案......
Sort of solution using metatable...
可以使用以下表示:
相关输出:
您还可以使用以下代码来检查布尔有效性:
输出为:
检查 表格教程了解更多信息。
The following representation can be used:
Related output:
You can also use the following code to check boolean validity:
The output is:
Check Tables Tutorial for additional information.
您可以使用此解决方案:
或
you can use this solution:
or
可以使用一个简单的函数:
上述代码的输出将是
A simple function can be used that :
output of above code would be