如何在tcl脚本中对列表列表进行分组
我有一个唯一id列表,员工id的子列表,如下所示:如何在tcl脚本中对列表列表进行分组,tcl,grouping,Tcl,Grouping,我有一个唯一id列表,员工id的子列表,如下所示: 5 {9 6} 6 {9 5} 14 {} 1 {8 2} 17 18 18 17 2 {8 1} 8 {2 1} 27 {} 4 {11 3} 3 {11 4} 7 11 11 {3 4 7} 9 {6 5} 22 {} {3 4 7 11} {5 6 9} {1 2 8} {17 18} 14 27 22 我试图通过如下连接对它们进行分组: 5 {9 6} 6 {9 5} 14 {} 1 {
5 {9 6}
6 {9 5}
14 {}
1 {8 2}
17 18
18 17
2 {8 1}
8 {2 1}
27 {}
4 {11 3}
3 {11 4}
7 11
11 {3 4 7}
9 {6 5}
22 {}
{3 4 7 11}
{5 6 9}
{1 2 8}
{17 18}
14
27
22
我试图通过如下连接对它们进行分组:
5 {9 6}
6 {9 5}
14 {}
1 {8 2}
17 18
18 17
2 {8 1}
8 {2 1}
27 {}
4 {11 3}
3 {11 4}
7 11
11 {3 4 7}
9 {6 5}
22 {}
{3 4 7 11}
{5 6 9}
{1 2 8}
{17 18}
14
27
22
根本不知道怎么做。希望有人能提供帮助。您应该遍历源代码列表并执行所需的操作。例如:
set dest [list]
foreach { id sub_ids } $src_list {
set found -1
set ids [concat $sub_ids [list $id]]
for { set i 0 } { $i < [llength $dest] } { incr i } {
set dest_rec [lindex $dest $i]
foreach { dest_id } $dest_rec {
if { $dest_id in $ids } {
set found $i
break
}
}
if { $found != -1 } {
break
}
}
if { $found == -1 } {
lappend dest [lsort -unique $ids]
} else {
set dest [lreplace $dest $found $found [lsort -unique [concat [lindex $dest $found] $ids]]]
}
}
set dest[列表]
foreach{id sub_id}$src_列表{
已找到集-1
设置id[concat$sub_id[list$id]]
对于{set i 0}{$i<[llength$dest]}{incr i}{
设置目的地记录[lindex$dest$i]
foreach{dest_id}$dest_rec{
如果{$dest_id in$ids}{
设置$i
打破
}
}
如果{$found!=-1}{
打破
}
}
如果{$found==-1}{
lappend dest[lsort-唯一$id]
}否则{
set dest[lreplace$dest$found$found[lsort-unique[concat[lindex$dest$found]$ids]]
}
}
到目前为止您尝试了什么?我尝试将列表合并成一个这样的列表:{596}{695}14 foreach a$t1{foreach{id assid}$a{lappend assid$id set assid1[lsort-unique$assid]如果{[lsearch$t2$assid1]=-1}{lappend t2[list$assid1]}设置assid{}}你应该把它作为一个编辑添加到你的问题中,而不是评论中。看来我的确认太早了。它现在似乎正在发挥作用。谢谢