Lua,表:合并重复键的值并删除重复项 [英] Lua, Tables: merge values of duplicate keys and remove duplicates
本文介绍了Lua,表:合并重复键的值并删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最近我开始对lua摆弄很多,但我一生都无法解决.
I've started fiddling a lot with lua recently, but I can't for my life figure this out.
假设我有一个看起来像这样的字符串:
Let's say I have a string that looks like this:
s ="a = x a = y b = z a = x"
s = "a=x a=y b=z a=x"
我想删除所有重复项并将重复键的值合并到一个表中,以便得到:
I want to remove all duplicates and merge the values of duplicate keys into a table, so that I get:
t = {
a = {x,y},
b = {z},
}
我考虑这件事已经太久了.感谢您的帮助!
I've been pondering about this for way too long. Any help is appreciated!
推荐答案
尝试一下:
s="a=x a=y b=z a=x"
s=s.." "
t={}
for k,v in s:gmatch("(.-)=(.-)%s+") do
if t[k]==nil then t[k]={} end
t[k][v]=true
end
for k,v in pairs(t) do
for z in pairs(v) do print(k,z) end
end
这篇关于Lua,表:合并重复键的值并删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文