For loop lua中的并行迭代
我想在Lua中并行地遍历多个表。我可以这样做:For loop lua中的并行迭代,for-loop,lua,iteration,torch,For Loop,Lua,Iteration,Torch,我想在Lua中并行地遍历多个表。我可以这样做: for i in range(#table1) pprint(table1[i]) pprint(table2[i]) end 但我更喜欢python的zip: for elem1, elem2 in zip(table1, table2): pprint(elem1) pprint(elem2) end 在标准Lua中是否有这样的东西(或者至少在torch附带的任何东西中?)如果您想在Lua中找到类似于Python函数的东西,
for i in range(#table1)
pprint(table1[i])
pprint(table2[i])
end
但我更喜欢python的zip
:
for elem1, elem2 in zip(table1, table2):
pprint(elem1)
pprint(elem2)
end
在标准Lua中是否有这样的东西(或者至少在torch附带的任何东西中?)如果您想在Lua中找到类似于Python函数的东西,您应该先看看。对于这种特殊情况,有一个函数。Penlight与Torch安装在一起,但您也可以通过Luarock获得它(Luarock与至少一个Torch分发捆绑在一起) 无论如何,Penlight中的
seq.zip
函数只支持压缩两个序列。这是一个更像Python的zip
的版本,即允许多于(或少于)两个序列:
local zip
do
local unpack = table.unpack or unpack
local function zip_select( i, var1, ... )
if var1 then
return var1, select( i, var1, ... )
end
end
function zip( ... )
local iterators = { n=select( '#', ... ), ... }
for i = 1, iterators.n do
assert( type( iterators[i] ) == "table",
"you have to wrap the iterators in a table" )
if type( iterators[i][1] ) ~= "number" then
table.insert( iterators[i], 1, -1 )
end
end
return function()
local results = {}
for i = 1, iterators.n do
local it = iterators[i]
it[4], results[i] = zip_select( it[1], it[2]( it[3], it[4] ) )
if it[4] == nil then return nil end
end
return unpack( results, 1, iterators.n )
end
end
end
-- example code (assumes that this file is called "zip.lua"):
local t1 = { 2, 4, 6, 8, 10, 12, 14 }
local t2 = { "a", "b", "c", "d", "e", "f" }
for a, b, c in zip( {ipairs( t1 )}, {ipairs( t2 )}, {io.lines"zip.lua"} ) do
print( a, b, c )
end
标准库中没有任何内容。我不知道火炬。但您可以编写自己的迭代器来轻松完成此操作。请查看Moses库:
--------------------------------------------------------------------------------
-- Python-like zip() iterator
--------------------------------------------------------------------------------
function zip(...)
local arrays, ans = {...}, {}
local index = 0
return
function()
index = index + 1
for i,t in ipairs(arrays) do
if type(t) == 'function' then ans[i] = t() else ans[i] = t[index] end
if ans[i] == nil then return end
end
return unpack(ans)
end
end
--------------------------------------------------------------------------------
-- Example use:
--------------------------------------------------------------------------------
a = {'a','b','c','d'}
b = {3,2,1}
c = {7,8,9,10,11}
for a,b,c,line in zip(a,b,c,io.lines(arg[0])) do
print(a,b,c,line)
end
print '\n--- Done! ---'