Home > Article > Backend Development > Application of Python List intersection, union, and difference set
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
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
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!