我在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
如果有更好的选择,请告诉我。
答案 0 :(得分:3)
假设t
中的值是连续的整数,则此代码应该起作用:
out1={}
out3={}
for k,v in pairs(t) do
out1[v]=k
out3[v]=v
end
请注意,即使t
中的对以非指定顺序访问,新表也会正确填充。
答案 1 :(得分:3)
假设任意键和值:
local keys, values = {}, {}
for k, v in pairs(input_table) do
keys[#keys+1] = k
values[#keys] = v
end
这是采用上述假设的最快方法 这样的表插入是基准测试并且证明更快,至少在香草Lua中。
答案 2 :(得分: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
答案 3 :(得分: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 Copyright (C) 1994-2008 Lua.org, PUC-Rio > track = require "q19953449" > t = track.new(true) > t.a = 1 > t.b = 2 > t.a = 0 > t.c = 3 > for k,v in track.history(t) do print(k,v) end a 1 b 2 a 0 c 3 > t.c = nil > for k,v in track.history(t) do print(k,v) end a 1 b 2 a 0 c 3 c userdata: 007FD638 > t.d = 0/0 > for k,v in track.history(t) do print(k,v) end a 1 b 2 a 0 c 3 c userdata: 007FD638 d userdata: 007FD658 > =t.d -1.#IND > =t.c nil > ^Z