Lua:以不区分大小写的方式合并2个字符串集合
我想以不区分大小写的方式合并两个字符串集合:Lua:以不区分大小写的方式合并2个字符串集合,lua,Lua,我想以不区分大小写的方式合并两个字符串集合: string_collection1 = {"hello","buddy","world","ciao"} string_collection2 = {"Hello","Buddy","holly","Bye", "bYe"} merged_string_collection = merge_case_insensitive(string_collection1,string_collection2) --> {"hello","buddy",
string_collection1 = {"hello","buddy","world","ciao"}
string_collection2 = {"Hello","Buddy","holly","Bye", "bYe"}
merged_string_collection = merge_case_insensitive(string_collection1,string_collection2) --> {"hello","buddy","world","holly","bye","ciao"}
这是一个尝试,但它不起作用
function merge_case_insensitive(t1,t2)
t3 = {}
for _,s1 in pairs(t1) do
for _,s2 in pairs(t2) do
if string.lower(s1) == string.lower(s2) then
t3[s1] = s1
end
end
end
t4 = {}
i = 1
for s,_ in pairs(t3) do
t4[i] = string.lower(s)
i = i + 1
end
return t4
end
string_collection1 = {"hello","buddy","world","ciao"}
string_collection2 = {"Hello","Buddy","holly","Bye", "bYe"}
merged_string_collection = merge_case_insensitive(string_collection1,string_collection2)
for k,v in pairs(merged_string_collection) do print(k,v) end
它不起作用,因为您使用==来比较两个区分大小写的字符串 您可以执行类似于
string.lower(s1)=string.lower(s2)
的操作来解决这个问题
编辑:
由于您自己无法理解其余部分,以下是一些代码:
local t1 = {"hello","buddy","world","ciao"}
local t2 = {"Hello","Buddy","holly","Bye", "bYe"}
local aux_table = {}
local merged_table = {}
for k,v in pairs(t1) do
aux_table[v:lower()] = true
end
for k,v in pairs(t2) do
aux_table[v:lower()] = true
end
for k,v in pairs(aux_table) do
table.insert(merged_table, k)
end
merged_table
现在包含两个输入表中每个单词的小写版本
现在,将它放入一个接受任意数量输入表的函数中,就完成了
我们在这里做的是:我们使用这些表中每个单词的小写版本,并将它们存储在一个列表中aux_table[string.lower(“Hello”)]
将索引与aux_table[string.lower(“Hello”)]
相同的值。因此,即使一个单词有多个变体,每个单词都有一个条目。
使用键可以省去比较字符串以及区分唯一单词和其他单词的麻烦。要获得一个表,其中两个其他表中的所有字符串只出现一次(不考虑大小写),您需要以下内容:
function merge_case_insensitive(t1,t2)
local ans = {}
for _,v in pairs(t1) do ans[v:lower()] = true end
for _,v in pairs(t2) do ans[v:lower()] = true end
return ans
end
string_collection1 = {"hello","buddy","world","ciao"}
string_collection2 = {"Hello","Buddy","holly","Bye", "bYe"}
merged_string_collection = merge_case_insensitive(string_collection1,string_collection2)
for k in pairs(merged_string_collection) do print(k) end
编辑:如果需要数组结果(无需添加另一个迭代)
我们只需在两个表上进行迭代,并存储一个临时字典来检查我们已经找到的单词,如果还没有找到,则将它们放入我们的新数组中:
function Merge(t1, t2)
local found = {} --Temporary dictionary
local new = {} --New array
local low --Value to store low versions of words in later
for i,v in ipairs(t1) do --Begin iterating over table one
low = v:lower()
if not found[low] then --If not found yet
new[#new+1] = low --Put it in the new table
found[low] = true --Add it to found
end
end
for i,v in ipairs(t2) do --Repeat with table 2
low = v:lower()
if not found[low] then
new[#new+1] = low
found[low] = true
end
end
return new --Return the new array
end
这种方法消除了第三次迭代的需要,就像在Pieget的答案中一样,并且不会像tonypdmtr的答案那样不断地重新定义函数和闭包并调用它们。当然可以。只有在两个表中都存在单词时,才能将它们添加到输出中。只需删除if语句,并在结果表中将这些单词用作小写键。我想我会留给你一些思考的东西。考虑在使用类似于数组的表时使用<代码> iBoe<代码>或数字循环。@ OKA,为什么我要这么做?@小猪的好习惯,并且是这个工作的适当功能。在其他上下文中保持秩序,这可能很重要。@Oka如果他的单词列表变得更复杂,可能包含一个空白,该怎么办?那你就完蛋了。我的好习惯是,只有当我100%确定不会有任何间隙或非数字键时,才使用ipairs。这太容易出错了。是什么让它成为这里的“适当功能”呢?为了以防万一,添加了它。不需要额外的迭代。
function Merge(t1, t2)
local found = {} --Temporary dictionary
local new = {} --New array
local low --Value to store low versions of words in later
for i,v in ipairs(t1) do --Begin iterating over table one
low = v:lower()
if not found[low] then --If not found yet
new[#new+1] = low --Put it in the new table
found[low] = true --Add it to found
end
end
for i,v in ipairs(t2) do --Repeat with table 2
low = v:lower()
if not found[low] then
new[#new+1] = low
found[low] = true
end
end
return new --Return the new array
end