Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 在TCL lsort中给出多重性计数的有效方法_Sorting_Tcl - Fatal编程技术网

Sorting 在TCL lsort中给出多重性计数的有效方法

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

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 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 
   }