集合a:1,2,3,4
集合b:3,4,5,6
在这两个集合中,a和b都拥有3和4。
a,b两个集合分别拥有的相同的元素集,称为a与b的交集。
返回两个或更多集合中都包含的元素,即交集。
a_set:在这里作为焦点集合。
a_set = {'name','xiaomu','xiaoming'}
b_set = {'xiaoming','xiaogang','xiaohong'}
a_inter = a_set.intersection(b_set)
print(a_inter)
运行结果:
{'xiaoming'}
# coding:utf-8
a = ['dewei','xiaomu','xiaohua','xiaoguo']
b = ['xiaohua','dewei','xiaoman','xiaolin']
c = ['xiaoguang','xiaobai','dewei','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)
xiaotou = list(result)
print('{}是这个小偷'.format(xiaotou[0]))
运行结果:
/Users/llq/PycharmProjects/pythonlearn/pythonlearn/python_set/bin/python /Users/llq/PycharmProjects/pythonlearn/python_set/set_inter.py
{'xiaomu', 'xiaohua', 'dewei', 'xiaoguo'} {'xiaohua', 'dewei', 'xiaoman', 'xiaolin'} {'xiaoyuan', 'dewei', 'xiaobai', 'xiaoguang'}
dewei是这个小偷
进程已结束,退出代码为 0