list TCL 从列表中删除一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5701947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:38:21  来源:igfitidea点击:

TCL remove an element from a list

listelementtcl

提问by Narek

How te remove an element from TCL list say:

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

  1. which has index = 4
  2. which has value = "aa"
  1. 其中索引 = 4
  2. 其中具有 value = "aa"

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

我用谷歌搜索过,还没有找到任何内置功能。

回答by drysdam

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

回答by Donal Fellows

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

删除元素的另一种方法是过滤掉它。这种 Tcl 8.5 技术与其他地方提到的lsearch&lreplace方法的不同之处在于它从列表中删除所有给定元素。

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 -indexoption.)

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

回答by icanhasserver

Lets say you want to replace element "b":

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

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

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

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

% lreplace $L 1 1
a c d

回答by Shawn E

regsubmay also be suitable to remove a value from a list.

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

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

regsub b $mylist "" mylist

puts $mylist
  a  c
llength $mylist
  2

回答by Todd Horst

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

回答by Antonio Mes

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