Home  >  Article  >  Backend Development  >  Application of Python List intersection, union, and difference set

Application of Python List intersection, union, and difference set

巴扎黑
巴扎黑Original
2017-04-14 10:14:072163browse

Generated two List:

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

The concepts of intersection, union, and difference set are not discussed here. The python code is as follows:

#! /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

Application of Python List intersection, union, and difference set

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

Generated two List:

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

The concepts of intersection, union and difference set are not discussed here. The python code is as follows:

#! /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

Application of Python List intersection, union, and difference set

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


The above is the detailed content of Application of Python List intersection, union, and difference set. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn