【发布时间】:2026-02-09 18:50:01
【问题描述】:
在将此问题标记为与this 重复之前,请阅读全文:
我在 Lua 中有一个表,它是一个类似于下面的表,我想删除其中的所有重复表。
table1 = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3, 4}}
我想要做的是删除重复的表并只使用一个。结果应该如下所示
table2 = {{1, 2, 3}, {1, 2, 3, 4}}
我尝试了很多我在网上找到的方法以及我自己的一些方法,但我无法做到。
这是我上次尝试的方法
local test = {1,2,4,2,3,4,2,3,4,"A", "B", "A"}
local hash = {}
local res = {}
for _,v in ipairs(test) do
if (not hash[v]) then
res[#res+1] = v -- you could print here instead of saving to result table if you wanted
hash[v] = true
end
end
-- Here the test is the input, and res is the output table without
-- any duplicates but this works only for values in it and not for
-- nested tables.
请帮帮我。
【问题讨论】:
-
除了在网上搜索解决方案,您真的考虑过这个问题吗?比如拿起笔和纸,一步一步地写下你必须做的事情?你不需要成为程序员来解决这样的问题。
-
是的,我做到了,给我时间到明天,我看看能不能得到答复。
-
在测试
hash[ser(v)]之前需要将v序列化成字符串
标签: lua duplicates lua-table nested-table