目录
- 什么是交集
- intersection的功能
- intersection的用法
什么是交集
- a , b两个集合分别拥有的相同的元素集,称为a与b的交集

intersection的功能
intersection的用法
- 用法:
a_set.intersection(b_set...)
- 参数:
b_set...
:与当前集合对比的1个或多个集合 - 返回值: 返回原始集合与对比集合的交集
In [32]: a_set = { ' name' , 'xiaomu', 'xiaoming' }
In [33]: b_set = { 'xiaoming' , 'xiaogang' , 'xiaohong' ]
In [34]: a_inter = a_set.intersection( b_set)
In [35]: a_inter
Out [35]: {'xiaoming' }
实战
a = ['insane', 'xiaomu', 'xiaohua', 'xiaoguo']
b = ['xiaohua', 'insane', 'xiaoman', 'xiaolin']
c = ['xiaoguang', 'xiaobai', 'insane', 'xiaoyuan']
a_set =set(a)
b_set = set(b)
c_set = set(c)
print(a_set, b_set, c_set)
result = a_set.intersection(b_set, c_set)
print(result)
xiaotou = list(result)
print('{} 是这个小偷'.format(xiaotou[0]))
{'xiaohua', 'xiaomu', 'xiaoguo', 'insane'} {'xiaohua', 'xiaoman', 'xiaolin', 'insane'} {'xiaoyuan', 'xiaoguang', 'xiaobai', 'insane'}
{'insane'}
insane 是这个小偷
Process finished with exit code 0