Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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
Arrays 在tcl中创建列表外数组_Arrays_List_Tcl - Fatal编程技术网

Arrays 在tcl中创建列表外数组

Arrays 在tcl中创建列表外数组,arrays,list,tcl,Arrays,List,Tcl,您好,我有一个父子项目列表,如下所示: set mylist {{1:0 2:0} {2:0 3:0} {3:0 4:0} {3:0 5:0} {3:0 6:0} {3:0 7:0} {4:0 8:0} {5:0 9:0} {4:0 10:0} {5:0 11:0}}; 现在,我正试图在这里完成两项任务 从上面的列表$mylist中创建唯一项目的新列表 创建一个数组,其中键作为我的新列表中的唯一项,值作为我可用的一些数据 因此,我使用下面的代码创建了一个新列表 set

您好,我有一个父子项目列表,如下所示:

set mylist {{1:0 2:0} {2:0 3:0} {3:0 4:0} {3:0 5:0} {3:0 6:0} {3:0 7:0}
            {4:0 8:0} {5:0 9:0} {4:0 10:0} {5:0 11:0}};
现在,我正试图在这里完成两项任务

  • 从上面的列表$mylist中创建唯一项目的新列表
  • 创建一个数组,其中键作为我的新列表中的唯一项,值作为我可用的一些数据 因此,我使用下面的代码创建了一个新列表

    set newlist [list];
    foreach item $mylist {
      lappend newlist [lindex $item 0]; 
      lappend newlist [lindex $item 1]; 
    }
    
    这给了我输出为

    1:0 2:0 2:0 3:0 3:0 4:0 3:0 5:0 3:0 6:0 3:0 7:0 4:0 8:0 5:0 9:0 4:0 10:0 5:0 11:0
    
    然后我做了一个独特的分类

    set newlist [lsort -unique $newlist];
    
    这给了我唯一的列表
    1:0 10:0 11:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0

    现在我创建如下数组

    array set newarr {
      [lindex $newlist 0] {list of values} 
      [lindex $newlist 1] {list of values} 
      [lindex $newlist 2] {list of values} 
      [lindex $newlist 3] {list of values} 
      ...
    }
    

    这基本上给了我想要达到的目标,但我想知道是否有更好的方法来完成同样的任务。例如,我在想是否有更好的方法从mylist创建newlist,基本上是从mylist项创建唯一的newlist

    在我脑海中,我可能会写下这样的话:

    foreach item [lsort -unique [concat {*}$mylist]] {
        set newarr($item) {list of values}
    }
    

    或者您可能更喜欢某些变量而不是嵌套命令。

    直接使用数组是获取唯一值列表的另一种方法(不能有重复的数组键)

    可以根据需要重新指定数组值

    % foreach pair $mylist {lassign $pair x y; incr ary($x); incr ary($y)}
    % parray ary
    ary(10:0) = 1
    ary(11:0) = 1
    ary(1:0)  = 1
    ary(2:0)  = 2
    ary(3:0)  = 5
    ary(4:0)  = 3
    ary(5:0)  = 3
    ary(6:0)  = 1
    ary(7:0)  = 1
    ary(8:0)  = 1
    ary(9:0)  = 1