您已经注意到{*}
(故意)没有深入两步。
您的特定示例并不能很好地说明问题,因此我建议这样做:
set a [list "ko" ]
set m [list [list "a b" "c d"] [list "e f" "g h"]]
lappend a {*}$m
在这里,我们a
设置为ko {{a b} {c d}} {{e f} {g h}}
. 这不是你想要的。但我们可以这样做:
lappend a {*}[concat {*}$m]
这给出了这个:ko {a b} {c d} {e f} {g h}
。看起来是对的。
但我们真的在这里做正确的事吗?让我们用我们的超级秘密内省器戳进去,representation
命令:
% tcl::unsupported::representation $m
value is a list with a refcount of 4, object pointer at 0x10085ec50, internal representation 0x103016790:0x0, string representation "{{a b} {c d}}..."
% tcl::unsupported::representation [concat {*}$m]
value is a string with a refcount of 1, object pointer at 0x10085de10, internal representation 0x1030052d0:0x10085f190, string representation "{a b} {c d} {..."
哦哦!我们已经失去了列表性。这不是一场灾难,但这不是我们想要的。相反,我们真的应该这样做:
foreach sublist $m {
lappend a {*}$sublist
}
好吧,那是更多的代码,但保留了列表性(如果你碰巧在叶子上有珍贵的类型,那会很好;核心 Tcl 没有这种珍贵的类型,但一些扩展有)。
我们可以比较时间:
% time {
set a [list "ko" ]
set m [list [list "a b" "c d"] [list "e f" "g h"]]
lappend a {*}[concat {*}$m]
} 10000
2.852789 microseconds per iteration
% time {
set a [list "ko" ]
set m [list [list "a b" "c d"] [list "e f" "g h"]]
foreach sublist $m {
lappend a {*}$sublist
}
} 10000
4.022959 microseconds per iteration
哦……</p>
% time {apply {{} {
set a [list "ko" ]
set m [list [list "a b" "c d"] [list "e f" "g h"]]
lappend a {*}[concat {*}$m]
}}} 10000
2.4486125 microseconds per iteration
% time {apply {{} {
set a [list "ko" ]
set m [list [list "a b" "c d"] [list "e f" "g h"]]
foreach sublist $m {
lappend a {*}$sublist
}
}}} 10000
1.6870501 microseconds per iteration
哈!类型正确的在过程中更好(类似上下文)。