取 List X 的最大值,并将其与 List Y 进行比较,找到 Y 中的最大值,使用 TCL 将其添加到新列表

Take the highest value of List X, and compare it with List Y ,and find the highest values in Y add it to new list using TCL

我有两个列表,比如说 X 和 Y。找到列表 X 的最高值,并将其与列表 Y 的值进行比较,如果 Y 的值大于 X,则使用 TCL 将其添加到新列表中.

set X[list 1.2156476714e-04 1.1284486163e-03 1.9818406145e-01 2.9287846814e-04 2.0217831320e-04]

set Y[list 1.2156476714e-04 1.1284486163e-03 4.5386226702e-02 4.4706815970e-02 8.4928524302e-03 6.0775778365e-03 3.1041158763e-03 1.5045881446e-01 4.1016753016e-04 1.1655993148e-03 1.8736355969e-03 2.9444883694e-02 2.5420340535e-02 2.0819682049e-03 9.5297318694e-03 8.5498101043e-04 1.5580140825e-02 8.0796216935e-03 4.8684447393e-02 1.6464813962e-01]

取List X的最高值,与List Y的每个值进行比较,如果List Y的值大于X的值,则将其添加到新列表中。

寻找事物的最大值是 max() 函数的事情(在 expr 中) 除了 我们想将它应用到列表所以我们直接调用函数的实现命令,这样我们就可以通过扩展输入值列表:

set max_X [tcl::mathfunc::max {*}$X]
#          ^^^^^^^^^^^^^^^    ^^^ Expansion: pass what follows as many arguments
#          Function calls get transformed into invokes of commands in the tcl::mathfunc namespace

对于第二个列表的过滤,写个程序就最清楚了。可以使用 foreachlmap 实现过滤;后者实际上只是一个 foreach,如果它们是 正常结果 而不是像 continue 这样的东西,它会收集值。

这两个版本的过程基本上做同样的事情:

proc filter_greater_than {value list} {
    lmap x $list {expr {$x > $value ? $x : [continue]}}
}
proc filter_greater_than {value list} {
    set result {}
    foreach x $list {
        if {$x > $value} {
            lappend result $x
        }
    }
    return $result
}

然后你使用这样的过程:

set new_list [filter_greater_than $max_X $Y]