這個操作應該是比較基礎的了,平時用得也比較少,所以寫在這裡提醒自己framework已經提供了這樣的功能了:
staticvoid main(string args)
;var right = new list;
var intersection = left.intersect(right);
console.writeline("
the intersection of left and right is:
", string.join("
,", intersection));//output: ownedbyboth
var differencesetofleft = left.except(right);
console.writeline("
the difference set of left to right is:
", string.join("
,", differencesetofleft));//output: ownedbyleft
var differencesetofright = right.except(left);
console.writeline("
the difference set of right to left is:
", string.join("
,", differencesetofright));//output: ownedbyright
var unionset = right.union(left);
console.writeline("
the union set of left and right is:
", string.join("
,", unionset));//output: ownedbyright, ownedbyboth, ownedbyleft
}private
enum sampledata
以上的示例中採用的是enum(同int),如果是對自定義的型別(如class、structure),則需要實現iequatable介面方能這樣用。
Python中list的交 並 差集獲取方法示例
1.獲取兩個list 的交集 coding utf 8 方法一 a 2,3,4,5 b 2,5,8 tmp val for val in a if val in b print tmp 2,5 方法二 print list set a intersection set b 2.獲取兩個list 的並...
順序表集合的交並差操作
1.用順序表表示集合,設計乙個演算法實現集合的求交集運算 void intersection sqlist a,sqlist b,sqlist c int i,j,k k記錄c中的元素個數 for i 0 i2.用順序表表示集合,設計乙個演算法實現集合的求並集運算 void union sqlist...
python列表的交 並 差集
list 的 sort 方法返回的是對已經存在的列表進行操作,而內建函式 sorted 方法返回的是乙個新的 list,而不是在原來的基礎上進行的操作 a.sort reverse true b sorted a,reverse false usr bin env python3 l1 1 2 3 ...