雅卡德距离-列表的并集和交集 [英] Jaccard distance - union and intersection of lists

查看:111
本文介绍了雅卡德距离-列表的并集和交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jaccard距离实现分层聚类.我尝试查找Jaccard的事务以二进制表示.例如:

I am implementing hierarchical clustering using Jaccard distance. The transactions for which I am trying to find Jaccard are represented in binary. For eg.:

t1=['0','1','1','0','1']

t2=['1','0','1','0','0'].

我看了这个SO问题,它与我想要的非常相似,但是我没有得到正确的答案.

I looked at this SO question, which is very similar to what I want, but I am not getting the right answer.

基本上,这就是我要寻找的内容:
1.找到以上两个列表的交集和并集.

Basically this is what I am looking for:
1. find intersection and union for the above 2 lists.

除了查看许多其他在线资源之外,我还尝试了以下方法:

I have tried the below apart from looking at numerous other online resources:

1. s1=sets.Set(['0','1','1','0','1'])
   s2=sets.Set(['1','0','1','0','0'])  
2. s1.intersection(s2)  ---> Set(['1', '0'])  
   s1.union(s2)         ---> Set(['1', '0'])  
3. Set(s1) & Set(s2)      ---> TypeError: unsupported operand type(s) for /: 'Set' and 'Set'

   Set(s1) | Set(s2)

请指导我.

谢谢.

推荐答案

如您所说:

s1=sets.Set(['0','1','1','0','1'])

让我们检查s1:

print s1
---->Set(['1', '0'])

sets 模块提供了用于构造和处理 unique 元素的无序集合的类.因此,您的s1和s2实际上是相同的.

sets module provides classes for constructing and manipulating unordered collections of unique elements. So, your s1 and s2 are actually the same.

这篇关于雅卡德距离-列表的并集和交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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