網頁

Python Two List Union, Intersect, Unique 兩個陣列的聯集,交集,取唯一值

Python Two List Union, Intersect, Unique  兩個陣列的聯集,交集,取唯一值

假設
list1=[0,1,2,0,1,2,3,4,5,6,7,8,9]
list2=[5,6,7,8,9,10,11,12,13,14]


  • 要取list1和list2的交集:
         list(set(list1) & set(list2))

  • 要取list1和list2的聯集:
         list(set(list1) | set(list2))

  • 要過濾list1中重覆的元素:
         list(set(list1))