產生了兩個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
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
if diff: print "wrong" else: print "matched"
以上是Python List交集,並集,差集的應用的詳細內容。更多資訊請關注PHP中文網其他相關文章!