2

我在 lua 有一张桌子

test = {fist=1,second=2,third=3}

我想将此表分成两个新表,其值为:

keys = {first, second, third}
values = {1,2,3}

我的尝试:

local keys = {}
local values = {}

for key , value in pairs(test) do
    table.insert(keys, key)
    table.insert(values, value)

end

如果有更好的选择,请告诉我。

4

4 回答 4

3

假设中的值t是连续整数,则此代码应该可以工作:

out1={}
out3={}
for k,v in pairs(t) do
        out1[v]=k
        out3[v]=v
end

请注意,即使t以未指定的顺序访问其中的对,新表也已正确填写。

于 2013-11-13T12:20:44.833 回答
3

假设任意键和值:

local keys, values = {}, {}

for k, v in pairs(input_table) do
    keys[#keys+1] = k
    values[#keys] = v
end

这是上述假设的最快方法。
至少在 vanilla Lua 中,此类表插入经过基准测试并被证明更快。

于 2013-11-13T12:26:45.943 回答
1

我的快速解决方案是这样的:

local function split_table(input_table)
    local out_table1 = {}
    local out_table2 = {}

    for key, value in pairs(input_table) do --simply iterating input table
        table.insert(out_table1, key)
        table.insert(out_table2, value)
    end

    return out_table1, out_table2 --returning both output tables
end
于 2013-11-13T12:21:45.720 回答
1

可以使用元表和影子表来制作历史跟踪表,记录添加和修改键的原始顺序。

这是一个经过轻微测试的示例,另存为track.lua

-- tracking table that keeps a history of keys in the
-- order they were added to the table.


-- safe markers to store to track the use of nil or NaN 
-- as either keys or values. Neither can be a key, and 
-- nil cannot be a value in a sequence. Note that the history
-- iterator will assume that the record of keys is a valid
-- sequence.
local nilmarker, nanmarker = newproxy(), newproxy()


-- Make a value that can server as either key or value in a
-- table, even if it is nil or NaN, neither of which can be
-- a table key or a value in a valid sequence.
local function safemark(v)
    if v == nil then return nilmarker end
    if v ~= v then return nanmarker end
    return v
end


-- Set a key and track it's history, potentially including
-- deletions since we use safe markers in the tracking tables.
local function t_newindex(t,k,v)
    local mt = getmetatable(t)
    if mt.__index ~= mt.shadow or not mt.shadow[k] then
        mt.k[#mt.k+1] = safemark(k)
        mt.v[#mt.v+1] = safemark(v)
    end
    mt.shadow[k] = v
    return mt and mt.shadow and mt.shadow[k]
end

-- Look up a key in the shadow table
local function t_index(t,k)
    return getmetatable(t).shadow[k]
end

-- simple module table
local tracked = {}

-- create a new table with tracked keys and values. If called
-- with no argument or false, only key creation and initial values
-- are tracked. If called with true, then every value change will
-- be tracked.
function tracked.new(fullhistory)
    local mt = {
        __newindex = t_newindex,
        shadow = {},
        k = {},
        v = {},
    }
    mt.__index = fullhistory and t_index or mt.shadow
    return setmetatable({},mt)
end


-- return a human-readable string describing a value,
-- paying attention to our private marks for nil and NaN
local function tracked.safe(v)
    if v == nilmarker then return "~~nil~~" end
    if v == nanmarker then return "~~nan~~" end
    return tostring(v)
end

-- return an iterator in history order of the keys and values
-- as they were created and updated. The history records nil
-- and NaN via private markers. To test for those markers, use
-- tracked.safe() to convert the possibly marked values to strings.
function tracked.history(t)
    local i = 0
    local mt = getmetatable(t)
    local k,v = mt.k, mt.v
    return function()
        i = i + 1
        return k[i], v[i]
    end
end

return tracked

假设它在模块路径中作为“track.lua”可用,那么它可以像这样使用:

C:\Users\Ross\Documents\tmp\SOQuestions>lua
Lua 5.1.4 版权所有 (C) 1994-2008 Lua.org, PUC-Rio
> 轨道 = 需要“q19953449”
> t = track.new(true)
> ta = 1
> 结核病 = 2
> ta = 0
> tc = 3
> for k,v in track.history(t) do print(k,v) end
一个 1
b 2
一个 0
3
> tc = 无
> for k,v in track.history(t) do print(k,v) end
一个 1
b 2
一个 0
3
c用户数据:007FD638
> 时差 = 0/0
> for k,v in track.history(t) do print(k,v) end
一个 1
b 2
一个 0
3
c用户数据:007FD638
d 用户数据:007FD658
> =td
-1.#IND
> =tc
零
> ^Z
于 2013-11-14T01:21:37.193 回答