在tcl中找到两个列表的交集 [英] Find the intersection of two lists in tcl

查看:50
本文介绍了在tcl中找到两个列表的交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个命令可以让我获得两个列表的交集(出现在两个列表中的成员)?

Is there a command that will allow me to get the intersection (members that apear in both lists) of two lists?

例如,如果列表是:

a b c d hc e f g h

结果应该是c h

推荐答案

假设你真正拥有的是一个集合(一个包含唯一元素的列表),你可以使用 tcllib:

Assuming what you really have is a set (a list with unique elements), you can use tcllib:

::struct::set 相交?set1... ?

package require Tcl 8.0
package require struct::set

set list1 {a b c d h}
set list2 {c e f g h}
::struct::set intersect $list1 $list2
> c h

这篇关于在tcl中找到两个列表的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆