列表去重以及两列表相同元素以及不同元素

列表

a=[1,2,3]

b=[3,4,5]

c=[1,1,2,2,3,3]

 

先看c去重.

list(set(c))

 

a与b交集

list(set(a).intersection(set(b)))

结果:[3]

 

a中有而b中没有的

list(set(a).difference(set(b)))

结果:[1, 2]

b中有而a中没有的

list(set(b).difference(set(a)))

 

a、b并集

list(set(b).union(set(a)))

 

posted @ 2017-03-10 21:47    阅读(235)  评论(0编辑  收藏  举报