欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

Python List交集,并集,差集的应用

程序员文章站 2022-06-09 22:16:05
...
生成了两个List:
A = ['apple','apple','banana']
B = ['banana','apple','banana']

交集,并集,差集概念这里不说,python代码如下:

#! /usr/bin/env python
# coding:utf-8

listA = [1, 2, 3, 4, 5, 6]
listB = [4, 5, 6, 7]

# Intersection
inte = list(set(listA).intersection(set(listB)))
print "Intersection:", inte

# union
uni = list(set(listA).union(set(listB)))
print "Union:", uni

# Differences
diff = list(set(listA).difference(set(listB)))
print "Differences:", diff

Python List交集,并集,差集的应用

if diff:
    print "wrong"
else:
    print "matched"

生成了两个List:

A = ['apple','apple','banana']
B = ['banana','apple','banana']

交集,并集,差集概念这里不说,python代码如下:

#! /usr/bin/env python
# coding:utf-8

listA = [1, 2, 3, 4, 5, 6]
listB = [4, 5, 6, 7]

# Intersection
inte = list(set(listA).intersection(set(listB)))
print "Intersection:", inte

# union
uni = list(set(listA).union(set(listB)))
print "Union:", uni

# Differences
diff = list(set(listA).difference(set(listB)))
print "Differences:", diff

Python List交集,并集,差集的应用

if diff:
    print "wrong"
else:
    print "matched"

以上就是Python List交集,并集,差集的应用的详细内容,更多请关注其它相关文章!