gpt4 book ai didi

python - 在 lua/torch 中枚举函数

转载 作者:行者123 更新时间:2023-11-28 18:30:50 25 4
gpt4 key购买 nike

在 python 中,我们使用 for i, _ in enumerate(wx): 其中 wx 是行矩阵或表格。我们如何在 lua/torch 中使用它。任何枚举函数?

最佳答案

在 Lua 中,你有 pairsipairs :

pairs (t)

If t has a metamethod __pairs, calls it with t as argument and returns the first three results from the call.

Otherwise, returns three values: the next function, the table t, and nil, 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 with nil in an empty table, next returns nil. If the second argument is absent, then it is interpreted as nil. In particular, you can use next(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.

关于python - 在 lua/torch 中枚举函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37606658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com