【发布时间】:2014-11-07 21:15:34
【问题描述】:
您好,我有一个父子项目列表如下:
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
然后我做了 lsort -unique
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 创建新列表,基本上是来自 mylist 项目的唯一新列表?
【问题讨论】: