2013-06-20 29 views
0

我有两个包含整数的不同长度的列表。 列表中现在有重复项。 查看两个列表时都有重复。 如从tcl中的两个列表中保存公共元素并将它们放在结果列表中

set ListA [list 3 4 9 1 2 10 6 ] 
set ListeB [list 34 43 9 12 2 10 61 88 23 48] 

是否有effektive快速的方法来创建一个新的列表,它仅包含现有的两个原始列表中的号码?在这种情况下:[9 2 10]

我不想使用嵌套循环,因为列表可能很大。我首先想到了对它们进行排序,然后逐个比较它们。然而,只有在两个列表的长度相同的情况下才有效。...

+0

的[查找TCL两个列表的交集]可能重复(http://stackoverflow.com/questions/12027197/find-the-intersection-of-two-lists-in-tcl) –

回答

2

看到这个questiondocumentation here。这对Tcl 8.0有效,仍然可以工作在8.5(我很确定8.6)。

% package require Tcl 
% package require struct::set 

% set ListA [list 3 4 9 1 2 10 6 ] 
% set ListeB [list 34 43 9 12 2 10 61 88 23 48] 
% ::struct::set intersect $ListA $ListeB 
9 2 10 
相关问题