Sorting 在TCL lsort中给出多重性计数的有效方法
TCL lsort没有提供项目多样性计数的良好特性。有没有快速的替代方案?我们正在查看约100万个对象的列表,其中有100个相同的条目Sorting 在TCL lsort中给出多重性计数的有效方法,sorting,tcl,Sorting,Tcl,TCL lsort没有提供项目多样性计数的良好特性。有没有快速的替代方案?我们正在查看约100万个对象的列表,其中有100个相同的条目 blasort -unique -count { 3 2 4 3 1 } 1 : 1 2 : 1 3 : 2 4 : 1 谢谢, Gert对于这样的元素计数,最好使用binsort派生的算法,使用字典或关联数组作为快速映射。即使有非常大的输入列表,以下内容也应该是有效的: proc countSort {elementList args} { set
blasort -unique -count { 3 2 4 3 1 }
1 : 1
2 : 1
3 : 2
4 : 1
谢谢,
Gert对于这样的元素计数,最好使用binsort派生的算法,使用字典或关联数组作为快速映射。即使有非常大的输入列表,以下内容也应该是有效的:
proc countSort {elementList args} {
set count {}
foreach element $elementList {
dict incr count $element
}
# Now sort the dictionary by the keys (i.e., the unique elements of the input)
return [lsort -stride 2 -index 0 {*}$args $count]
}
通过从正在使用的输入列表中复制问题中的输出,演示如何使用它:
set input { 3 2 4 3 1 }
foreach {item count} [countSort $input] {
puts "$item : $count"
}
老式的使用数组
proc count { lista } {
foreach item $lista {
incr counter($item)
}
return [array get counter];# return a list
;# or showing the result
;#parray counter
}