Skip to main content

Python 集合的交集--intersection函数

Published 1 min readMarkdown
Python 集合的交集--intersection函数

集合的交集--intersection函数

什么是交集

  • a , b两个集合分别拥有的相同的元素集 , 称为a与b的交集
  • 1

功能

  • 返回两个或更多集合中都包含的元素,即交集

用法

  • a_set.intersection(b_set...)

参数

b_set...: 与当前集合对比的1或多个集合

返回值

  • 返回原始集合与对比集合的交集

代码

# 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]))

No comments yet. Be the first to comment.

More from this blog