Python List,Set的交集,并集,差集与对称差集

1.python List交集、并集、差集

1). 获取两个list 的交集

方法一:

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 的并集

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

3). 获取两个 list 的差集

print list(set(b).difference(set(a))) # b中有而a中没有的

2.python Set交集、并集、差集

s = set([3,5,9,10,20,40])      #创建一个数值集合 

t = set([3,5,9,1,7,29,81])      #创建一个数值集合 

a = t | s          # t 和 s的并集 ,等价于t.union(s)

b = t & s          # t 和 s的交集 ,等价于t.intersection(s) 

c = t - s          # 求差集(项在t中,但不在s中)  ,等价于t.difference(s) 

d = t ^ s          # 对称差集(项在t或s中,但不会同时出现在二者中),等价于t.symmetric_difference(s)

Python,Numpy,Pandas的性对比测试


#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Randy'
import pandas as pd
import numpy as np
import timeit

l1=range(1,100000,3)
l2=range(0,100000,7)

def test1():
    # 2.53528213501
    [_ for _ in l1 if _ not in l2]

def test2():
    '''
    c = t - s  求差集(项在t中,但不在s中)  ,等价于t.difference(s)
    :return:
    '''
    # 0.00369501113892 100:0.345936059952
    list(set(l1) - set(l2))

def test2():
    '''
    c = t - s  求差集(项在t中,但不在s中)
    :return:
    '''
    # 0.00439596176147 100:0.320991039276
    set(l1).difference(set(l2))

def test4():
    # 0.00449991226196 100: 0.406269073486
    np.setdiff1d(np.array(l1),np.array(l2))

def test5():
    '''
    d = t ^ s 对称差集(项在t或s中,但不会同时出现在二者中),等价于t.symmetric_difference(s)
    :return:
    '''
    set(l1) ^ set(l2)

def test6():
    '''
    d = t ^ s 对称差集(项在t或s中,但不会同时出现在二者中
    :return:
    '''
    set(l1).symmetric_difference(set(l2))

def test7():
    # 0.00773096084595  100: 0.708806991577
    idx1 = pd.Index(l1)
    idx2 = pd.Index(l2)
    idx1.difference(idx2)

if __name__ == "__main__":
    TEST_NUM = 100
    print 'test 1 time:', timeit.timeit('test1()','from __main__ import test1',number=TEST_NUM)
    print 'test 2 time:', timeit.timeit('test2()','from __main__ import test2',number=TEST_NUM)
    print 'test 3 time:', timeit.timeit('test3()','from __main__ import test3',number=TEST_NUM)
    print 'test 4 time:', timeit.timeit('test4()','from __main__ import test4',number=TEST_NUM)
    print 'test 5 time:', timeit.timeit('test5()','from __main__ import test5',number=TEST_NUM)
    print 'test 6 time:', timeit.timeit('test6()','from __main__ import test6',number=TEST_NUM)
    print 'test 7 time:', timeit.timeit('test7()','from __main__ import test7',number=TEST_NUM)

测试结果如下:

test 1 time: 248.930391073
test 2 time: 0.362827062607
test 3 time: 0.337806940079
test 4 time: 0.350434064865
test 5 time: 0.334094047546
test 6 time: 0.331310033798
test 7 time: 0.746217012405


相关推荐


评论(0条)

暂时还没有评论,第一个来评论吧!


我要发表看法

引用   粗体   链接   缩进  

最近编辑

热门标签