TCL 从列表中删除一个元素

发布于 2024-11-02 04:56:52 字数 110 浏览 0 评论 0原文

如何从 TCL 列表中删除一个元素说:

  1. 其索引 = 4
  2. 其值 =“aa”

我已经用 Google 搜索过,但尚未找到任何内置函数。

How te remove an element from TCL list say:

  1. which has index = 4
  2. which has value = "aa"

I have Googled and have not found any built-in function yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

饮湿 2024-11-09 04:56:52
set mylist {a b c}
puts $mylist
a b c

按索引删除

set mylist [lreplace $mylist 2 2]
puts $mylist 
a b

按值删除

set idx [lsearch $mylist "b"]
set mylist [lreplace $mylist $idx $idx]
puts $mylist
a
set mylist {a b c}
puts $mylist
a b c

Remove by index

set mylist [lreplace $mylist 2 2]
puts $mylist 
a b

Remove by value

set idx [lsearch $mylist "b"]
set mylist [lreplace $mylist $idx $idx]
puts $mylist
a
阿楠 2024-11-09 04:56:52

删除元素的另一种方法是将其过滤掉。此 Tcl 8.5 技术与其他地方提到的 lsearch&lreplace 方法不同,因为它从列表中删除给定元素的全部

set stripped [lsearch -inline -all -not -exact $inputList $elemToRemove]

它不做的是搜索嵌套列表。这是 Tcl 没有花精力太深入地理解数据结构的结果。 (不过,您可以通过 -index 选项告诉它通过比较子列表的特定元素来进行搜索。)

The other way to remove an element is to filter it out. This Tcl 8.5 technique differs from the lsearch&lreplace method mentioned elsewhere in that it removes all of a given element from the list.

set stripped [lsearch -inline -all -not -exact $inputList $elemToRemove]

What it doesn't do is search through nested lists. That's a consequence of Tcl not putting effort into understanding your data structures too deeply. (You can tell it to search by comparing specific elements of the sublists though, via the -index option.)

空心↖ 2024-11-09 04:56:52

假设您想要替换元素“b”:

% set L {a b c d}
a b c d

您可以将第一个元素 1 和最后一个元素 1 替换为空:

% lreplace $L 1 1
a c d

Lets say you want to replace element "b":

% set L {a b c d}
a b c d

You replace the first element 1 and last element 1 by nothing:

% lreplace $L 1 1
a c d
酒与心事 2024-11-09 04:56:52

regsub 也可能适合从列表中删除值。

set mylist {a b c}
puts $mylist
  a b c

regsub b $mylist "" mylist

puts $mylist
  a  c
llength $mylist
  2

regsub may also be suitable to remove a value from a list.

set mylist {a b c}
puts $mylist
  a b c

regsub b $mylist "" mylist

puts $mylist
  a  c
llength $mylist
  2
同展鸳鸯锦 2024-11-09 04:56:52

刚刚完成了别人所做的事情

proc _lremove {listName val {byval false}} {
    upvar $listName list

    if {$byval} {
        set list [lsearch -all -inline -not $list $val]
    } else {
        set list [lreplace $list $val $val]
    }

    return $list
}

然后打电话

Inline edit, list lappend
    set output [list 1 2 3 20]
    _lremove output 0
    echo $output
    >> 2 3 20

Set output like lreplace/lsearch
    set output [list 1 2 3 20]
    echo [_lremove output 0]
    >> 2 3 20

Remove by value
    set output [list 1 2 3 20]
    echo [_lremove output 3 true]
    >> 1 2 20

Remove by value with wildcar
    set output [list 1 2 3 20]
    echo [_lremove output "2*" true]
    >> 1 3

Just wrapped up what others have done

proc _lremove {listName val {byval false}} {
    upvar $listName list

    if {$byval} {
        set list [lsearch -all -inline -not $list $val]
    } else {
        set list [lreplace $list $val $val]
    }

    return $list
}

Then call with

Inline edit, list lappend
    set output [list 1 2 3 20]
    _lremove output 0
    echo $output
    >> 2 3 20

Set output like lreplace/lsearch
    set output [list 1 2 3 20]
    echo [_lremove output 0]
    >> 2 3 20

Remove by value
    set output [list 1 2 3 20]
    echo [_lremove output 3 true]
    >> 1 2 20

Remove by value with wildcar
    set output [list 1 2 3 20]
    echo [_lremove output "2*" true]
    >> 1 3
甜是你 2024-11-09 04:56:52

你也可以这样尝试:

set i 0
set myl [list a b c d e f]

foreach el $myl {
   if {$el in {a b e f}} {
      set myl [lreplace $myl $i $i]
   } else {
      incr i
   }
}
set myl

You can also try like this :

set i 0
set myl [list a b c d e f]

foreach el $myl {
   if {$el in {a b e f}} {
      set myl [lreplace $myl $i $i]
   } else {
      incr i
   }
}
set myl
太阳男子 2024-11-09 04:56:52

有两种简单的方法。

# index
set mylist "a c b"
set mylist [lreplace $mylist 2 2]
puts $mylist 
a b
    
# value
set idx [lsearch $mylist "b"]
set mylist [lreplace $mylist $idx $idx]
puts $mylist
a

There are 2 easy ways.

# index
set mylist "a c b"
set mylist [lreplace $mylist 2 2]
puts $mylist 
a b
    
# value
set idx [lsearch $mylist "b"]
set mylist [lreplace $mylist $idx $idx]
puts $mylist
a
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文