我有一个表,我在其中传递名称:
names = {'Sachin', 'Ponting', 'Dhoni'}
在其他表格中,我传递了国家名称:
country = {"India", "Australia", "India"}
我想要像这样的输出表:
out_table = {Sachin="India", Ponting="Australia", Dhoni="India"}
这是一个直接的尝试:
names = {'Sachin', 'Ponting', 'Dhoni'}
countries = {"India", "Australia", "India"}
function table_map(names, countries)
local out = {}
for i, each in ipairs(names) do
out[each] = countries[i]
end
return out
end
out_table = table_map(names, countries)
您可以创建一个从两个序列中获取值的新迭代器:
function both_values(t1, t2)
local i = 0
return function() i = i + 1; return t1[i], t2[i] end
end
然后像这样使用迭代器:
local out_table = {}
for v1, v2 in both_values(names, country) do
out_table[v1] = v2
end