iTesting软件测试知识分享

Python 求List的交集,并集,差集.

Python 求List的交集,并集,差集

1.获取两个list 的交集

#方法一:

1
2
3
4
5
a=[126345]
b=[2, 5, 12]
temp = [i for i in a if i in b]
print temp
#[2,5]

#方法二

1
print list(set(a).intersection(set(b)))
2.获取两个list 的并集
1
print list(set(a).union(set(b)))

3.获取两个 list 的差集

1
print list(set(b).difference(set(a)))

扩展阅读:
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消 除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 sysmmetric difference(对称差集)等数学运算:

Operation Equivalent Result
s.update(t) s |= t return set s with elements added from t
s.intersection_update(t) “s &= t” return set s keeping only elements also found in t
s.difference_update(t) “s -= t” return set s after removing elements found in t
s.symmetric_difference_update(t) “s ^= t return set s with elements from s or t but not both
s.add(x) add element x to set s
s.remove(x) remove x from set s; raises KeyError if not present
s.discard(x) removes x from set s if present
s.pop() remove and return an arbitrary element from s; raises KeyError if empty
s.clear() remove all elements from set s
🐶 您的支持将鼓励我继续创作 🐶
-------------评论, 吐槽, 学习交流,请关注微信公众号 iTesting-------------
iTesting wechat
扫码关注,跟作者互动