Lua中如何检查表是否包含某个元素?
有没有一种方法可以检查表是否包含值?我有自己的(天真的)功能,但我想知道是否存在“官方”功能?或者更有效的东西......
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
顺便说一句,我使用这个函数的主要原因是将表用作集合,即没有重复的元素。还有其他我可以使用的东西吗?
Is there a method for checking if a table contains a value ? I have my own (naive) function, but I was wondering if something "official" exists for that ? Or something more efficient...
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
By the way, the main reason I'm using this functions is to use tables as sets, ie with no duplicate elements. Is there something else I could use ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将值作为表的键。例如:
这里有一个功能更齐全的示例。
You can put the values as the table's keys. For example:
There's a more fully-featured example here.
给定你的表示,你的函数是尽可能高效的。当然,正如其他人所指出的(以及早于 Lua 的语言中的实践),真正问题的解决方案是改变表示。当您有表并且需要集合时,可以通过使用 set 元素作为键并使用
true
作为值将表转换为集合。 +1 为 interjay。Given your representation, your function is as efficient as can be done. Of course, as noted by others (and as practiced in languages older than Lua), the solution to your real problem is to change representation. When you have tables and you want sets, you turn tables into sets by using the set element as the key and
true
as the value. +1 to interjay.我知道这是一篇旧文章,但我想为后代添加一些内容。
处理您遇到的问题的简单方法是创建另一个表,将值作为键。
IE。您有两张具有相同值的表,一张指向一个方向,一张指向另一个方向。
然后,您可以查询新表以查看它是否具有键“element”。这样就无需遍历另一个表的每个值。
如果事实证明您实际上无法使用“元素”作为键,因为它不是字符串,那么可以在其上添加校验和或
tostring
,然后将其用作关键。你为什么要这样做?如果您的表非常大,则迭代每个元素的时间将很长,从而阻止您经常这样做。额外的内存开销相对较小,因为它将存储指向同一对象的 2 个指针,而不是同一对象的 2 个副本。
如果您的表非常小,那么它的影响就会小得多,事实上,迭代甚至可能比进行另一次地图查找更快。
然而,问题的措辞强烈表明您有大量的项目需要处理。
I know this is an old post, but I wanted to add something for posterity.
The simple way of handling the issue that you have is to make another table, of value to key.
ie. you have 2 tables that have the same value, one pointing one direction, one pointing the other.
You can then query the new table to see if it has the key 'element'. This prevents the need to iterate through every value of the other table.
If it turns out that you can't actually use the 'element' as a key, because it's not a string for example, then add a checksum or
tostring
on it for example, and then use that as the key.Why do you want to do this? If your tables are very large, the amount of time to iterate through every element will be significant, preventing you from doing it very often. The additional memory overhead will be relatively small, as it will be storing 2 pointers to the same object, rather than 2 copies of the same object.
If your tables are very small, then it will matter much less, infact it may even be faster to iterate than to have another map lookup.
The wording of the question however strongly suggests that you have a large number of items to deal with.
我想不出另一种方法来比较值,但如果使用集合的元素作为键,则可以将值设置为 nil 以外的任何值。然后您就可以快速查找,而无需搜索整个表。
I can't think of another way to compare values, but if you use the element of the set as the key, you can set the value to anything other than nil. Then you get fast lookups without having to search the entire table.