如果List1是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25array1是:
{3 4 5} {12 13} {20 21}如何通过替换list1的每个元素的反向列表(即产生此输出)来根据array1转换array1:
1 2 5 4 3 6 7 8 9 10 11 13 12 14 15 16 17 18 19 21 20 22 23 24 25 ^^^^^ ^^^^^ ^^^^^发布于 2016-03-14 09:55:44
这不是排序任务,而是搜索任务。
如果假设要反转的范围不重叠,但也不一定存在(即不使用它们是连续数字的事实),则会得到如下内容:
# Iterate over each of the replacement patterns
foreach range $array1 {
# Iterate over each of the locations where the first element of the current
# replacement pattern is found
foreach pos [lsearch -all -exact $list1 [lindex $range 0]] {
# This will be the index of the *last* element in each subrange
set pos2 [expr {$pos + [llength $range] - 1}]
# Do the reversed replacement if the ranges match
if {[lrange $list1 $pos $pos2] eq $range} {
set list1 [lreplace $list1 $pos $pos2 {*}[lreverse $range]]
}
}
}之后的结果将出现在更新的list1变量中。将其包装在一个过程中是一项练习。
https://stackoverflow.com/questions/35983750
复制相似问题