for i, _ in enumerate(wx):
,其中wx
是一个行矩阵或表格。在Lua/Torch中,我们如何使用它?有任何枚举函数吗?pairs
和ipairs
:
你也可以使用pairs (t)
If
t
has a metamethod__pairs
, calls it witht
as argument and returns the first three results from the call.Otherwise, returns three values: the
next
function, the tablet
, andnil
, so that the construction
for k,v in pairs(t) do body end
will iterate over all key–value pairs of table
t
.
next
,来创建自定义枚举:
next (table [, index])
Allows a program to traverse all fields of a table. Its first argument is a table and its second argument is an index in this table. next returns the next index of the table and its associated value. When called with
nil
as its second argument,next
returns an initial index and its associated value. When called with the last index, or withnil
in an empty table,next
returns nil. If the second argument is absent, then it is interpreted as nil. In particular, you can usenext(t)
to check whether a table is empty.The order in which the indices are enumerated is not specified, even for numeric indices. (To traverse a table in numerical order, use a numerical for.)
The behavior of
next
is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields. In particular, you may clear existing fields.
a= { 1, 2, 3 }
。 - hjpotter92a={1,2,3} for i,v in ipairs(a) do print(i,v) end
- moteusa
不是一个表格,而是一个二维张量,因此请使用apply
或使用a:totable()
进行转换。另请参阅:https://dev59.com/QZHea4cB1Zd3GeqPt8l8 - deltheil