我有一个列表变量 $a,它的值低于。
{1|Katy|347689 2|Jerry|467841 1|Katy|987654}
我正在尝试删除重复的
1|Katy avoiding the userid available at last.
预期的输出应该是。
{1|Katy|347689 2|Jerry|467841}
我尝试使用 lsort -unique 选项。在我的情况下,这似乎无法正常工作。
set uniqueElement [lsort -unique $a]
此外,仅出于说明目的,列表值显示为具有 3 个值。我有超过 500 个相同格式。我试图在 1|Katy 的基础上删除重复项,同时避免使用 userid。 可以建议我可以解决此问题的任何其他方法以删除列表中这种格式的重复项吗?
答案 0 :(得分:0)
这有点棘手,因为您有一些在重复数据删除时要忽略的部分。因此,lsort -unique
不是合适的工具。相反,您想使用字典。
# Identify the values that each key maps to
set d {}
foreach entry $inputList {
# Alternatively, use regular expressions to do the entry parsing
set value [join [lassign [split $entry "|"] a b] "|"]
set key [string cat $a "|" $b]
dict lappend d $key $value
}
# Build the output list using the first value each key maps to
set outputList {}
dict for {key values} $d {
lappend outputList [string cat $key "|" [lindex $values 0]]
}
这使得 outputList
拥有您正在寻求的价值。 (您不需要使用 string cat
,但我认为在这种情况下它会使代码更清晰。)