方法一:
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)))
print list(set(a).union(set(b)))
print list(set(b).difference(set(a))) # b中有而a中没有的
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)
#!/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
stat -c "%a %n" file , 755, 644
ModuleNotFoundError: No module named 'MySQLdb', ...
Ubuntu18.04下使用pyenv安装python报错:BUILD FAILED (Ubun ...
dell提供了工具远程查看服务器硬件状态,可以修改硬件的参数,可以远程连接过去直接修改,就不需要 ...
启动报undefined symbol: EVP_CIPHER_CTX_cleanup错误。op ...
ubuntu开启crontab日志记录及解决No MTA installed, discardi ...
暂时还没有评论,第一个来评论吧!