如何从Tcl列表中删除重复元素说:
list is like [this,that,when,what,when,how]
Run Code Online (Sandbox Code Playgroud)
我用Google搜索并找到了,lsort unique
但同样对我不起作用.我想从列表中删除.
小智 12
以下适用于我
set myList [list this that when what when how]
lsort -unique $myList
Run Code Online (Sandbox Code Playgroud)
这回来了
how that this what when
Run Code Online (Sandbox Code Playgroud)
您可以将其存储在新列表中
set uniqueList [lsort -unique $myList]
Run Code Online (Sandbox Code Playgroud)
您还可以使用字典,其中键必须是唯一的:
set l {this that when what when how}
foreach element $l {dict set tmp $element 1}
set unique [dict keys $tmp]
puts $unique
Run Code Online (Sandbox Code Playgroud)
this that when what how
Run Code Online (Sandbox Code Playgroud)
这将保留元素的顺序.
归档时间: |
|
查看次数: |
17901 次 |
最近记录: |