首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >tcl:排序列表取决于数组

tcl:排序列表取决于数组
EN

Stack Overflow用户
提问于 2016-03-14 09:28:58
回答 1查看 38关注 0票数 0

如果List1是:

代码语言:javascript
运行
复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

array1是:

代码语言:javascript
运行
复制
{3 4 5} {12 13} {20 21}

如何通过替换list1的每个元素的反向列表(即产生此输出)来根据array1转换array1

代码语言:javascript
运行
复制
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
代码语言:javascript
运行
复制
    ^^^^^               ^^^^^                   ^^^^^
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-14 09:55:44

这不是排序任务,而是搜索任务。

如果假设要反转的范围不重叠,但也不一定存在(即不使用它们是连续数字的事实),则会得到如下内容:

代码语言:javascript
运行
复制
# 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变量中。将其包装在一个过程中是一项练习。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35983750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档