Elegant way to compare sequences

前端 未结 8 1910
死守一世寂寞
死守一世寂寞 2021-02-05 20:10

Does python provide an elegant way to check for \"equality\" of sequences of different types? The following work, but they seem rather ugly and verbose for python code:

8条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-05 20:36

    It looks like tuple(a) == tuple(b) is the best overall choice. Or perhaps tuple comparison with a preceding len check if they'll often be different lengths. This does create extra lists, but hopefully not an issue except for really huge lists. Here is my comparison of the various alternatives suggested:

    import timeit
    
    tests = (
    '''
    a=b=[5]*100
    ''',
    
    '''
    a=[5]*100
    b=[5]*3
    ''',
    
    '''
    a=b=(5,)*100
    ''',
    
    '''
    a=b="This on is a string" * 5
    ''',
    
    '''
    import array
    a=b=array.array('B', "This on is a string" * 5)
    '''
    )
    
    common = '''import itertools
    def comp1(a, b):
        if len(a) != len(b):
            return False
        for i, v in enumerate(a):
            if v != b[i]:
                return False
        return True'''
    
    for i, setup in enumerate(tests):
        t1 = timeit.Timer("comp1(a, b)", setup + common)
        t2 = timeit.Timer("all(x == y for x, y in itertools.izip_longest(a, b))", setup + common)
        t3 = timeit.Timer("all([x == y for x, y in itertools.izip_longest(a, b)])", setup + common)
        t4 = timeit.Timer("list(a) == list(b)", setup + common)
        t5 = timeit.Timer("tuple(a) == tuple(b)", setup + common)
    
        print '==test %d==' % i
        print '   comp1: %g' % t1.timeit()
        print ' all gen: %g' % t2.timeit()
        print 'all list: %g' % t3.timeit()
        print '    list: %g' % t4.timeit()
        print '   tuple: %g\n' % t5.timeit()
    

    Here are the results:

    ==test 0==
       comp1: 27.8089
     all gen: 31.1406
    all list: 29.4887
        list: 3.58438
       tuple: 3.25859
    
    ==test 1==
       comp1: 0.833313
     all gen: 3.8026
    all list: 33.5288
        list: 1.90453
       tuple: 1.74985
    
    ==test 2==
       comp1: 30.606
     all gen: 31.4755
    all list: 29.5637
        list: 3.56635
       tuple: 1.60032
    
    ==test 3==
       comp1: 33.3725
     all gen: 35.3699
    all list: 34.2619
        list: 10.2443
       tuple: 10.1124
    
    ==test 4==
       comp1: 31.7014
     all gen: 32.0051
    all list: 31.0664
        list: 8.35031
       tuple: 8.16301
    

    Edit: Added a few more tests. This was run on an AMD 939 3800+ with 2GB of ram. Linux 32bit, Python 2.6.2

提交回复
热议问题