我试图将前面的数字合并到一个数字列表中,比如1-14,在列表15-43之前。我正在使用TCL编写代码。
我希望列表应该是1,2,3,。。。。43,相反,名单是15,16,17,。。。43.
我试着将丢失的数字合并如下:
set nres2 ""
set x 0
set y [lindex $nres $x]
while {$x < [llength $nres]} {
set i [lindex $nres $x]
while {$y < $i} {
lappend nres2 $y
incr y
}
incr x
incr y
}
这将在列表中包含缺失的数字,例如,如果列表15、16、17…、43没有18、22、34等数字,则将这些数字包含在名为nres2的单独列表中。
但是,我不能包括相应列表的前面数字。任何意见/建议都将非常有帮助。
提前感谢。。。Prathit Chatterjee
我会在开始时更改y
:
set nres2 ""
set x 0
set y 1
while {$x < [llength $nres]} {
set i [lindex $nres $x]
while {$y < $i} {
lappend nres2 $y
incr y
}
incr x
incr y
}
有了nres作为4 6 8 9
,nres2就变成了1 2 3 5 7
,这就是我认为你想要得到的。
Jerry已经发布了即时修复,但您可能会考虑使用成对比较对实现进行总体改进:
proc printMissings1 {lst} {
set b [lrepeat [lindex $lst end] 0]
foreach i $lst {
lset b $i 1
}
lsearch -exact -integer -all $b 0
}
printMissings1 $nres
lrepeat
用于创建[llength $nres]
元素的Tcl列表(数组(- CCD_ 6用于标记其索引对应于给定范围中的值的元素
lsearch
用于返回未标记元素的索引,这些索引对应于缺失的值