Application of Python List intersection, union, and difference set

巴扎黑
Release: 2017-04-14 10:14:07
Original
2162 people have browsed it

Generated two List:

A = ['apple','apple','banana']
B = ['banana','apple','banana']
Copy after login
Copy after login

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
Copy after login
Copy after login

Application of Python List intersection, union, and difference set

if diff:
    print "wrong"
else:
    print "matched"
Copy after login
Copy after login

Generated two List:

A = ['apple','apple','banana']
B = ['banana','apple','banana']
Copy after login
Copy after login

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
Copy after login
Copy after login

Application of Python List intersection, union, and difference set

if diff:
    print "wrong"
else:
    print "matched"
Copy after login
Copy after login


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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!