18
A
回答
34
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
4
比方說,你要替換元素 「B」:
% set L {a b c d}
a b c d
您可以通過任何更換第一器件和最後一個元素1:
% lreplace $L 1 1
a c d
15
另一種刪除元素的方法是將其過濾掉。這種Tcl 8.5技術不同於其他地方提到的方法,因爲它從列表中刪除給定元素的全部。
set stripped [lsearch -inline -all -not -exact $inputList $elemToRemove]
它沒有做的是通過嵌套列表進行搜索。這是Tcl沒有努力深入理解您的數據結構的結果。 (你可以告訴它通過比較雖然子列表中的具體內容進行搜索,通過-index
選項。)
1
regsub
也可以適用於從列表中刪除值。
set mylist {a b c}
puts $mylist
a b c
regsub b $mylist "" mylist
puts $mylist
a c
llength $mylist
2
0
剛剛結束了別人怎麼做
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
相關問題
- 1. 從tcl列表中刪除子列表
- 2. 刪除元素,並從列表中刪除下列元素
- 3. 從元組列表中刪除元組
- 4. 從元組列表中刪除元素
- 5. 如何從列表中刪除元素
- 6. 從列表中刪除元素
- 7. 從列表中動態刪除元素
- 8. 從列表中增量刪除元素
- 9. 從標準列表中刪除元素
- 10. 從鏈接列表中刪除元素
- 11. 元素不從列表中刪除
- 12. 從列表中刪除偶數元素
- 13. 從C++列表中刪除元素
- 14. 從鏈接列表中刪除元素
- 15. 從python列表中刪除元素
- 16. 從鏈接列表中刪除元素
- 17. 從python3的列表中刪除元素
- 18. 從列表中刪除當前元素
- 19. 從通用列表中刪除元素
- 20. 從列表中刪除元素
- 21. 從內核列表中刪除元素
- 22. 從列表元素中刪除項目?
- 23. 從元組中刪除逗號列表
- 24. 從動態列表中刪除元素
- 25. 從R列表中刪除元素
- 26. python-從列表中刪除元素
- 27. 如何有條件地從元組列表中刪除元素?
- 28. 從列表中刪除值從列表中刪除
- 29. 文件沒有刪除TCL
- 30. 結合元組和列表的列表,從列表中刪除元組元素
這是偉大的,但我的名單由來自子列表打電話,我想在每個子表,如果檢查第二個索引中的值爲0(例如),我想刪除該子列表。我應該怎麼做。附:問題是[lindex $ list]返回的值不是參考 – Narek 2011-04-18 11:27:54
@Narek然後請更新您的問題,問問你想要什麼,B/C drysdam回答了你問的問題。 – 2011-04-18 12:20:31
請將[lreplace $ idx $ idx]更改爲[lreplace $ mylist $ idx $ idx]。 – Narek 2011-05-11 05:10:14