Match two lists of letters in Python

后端 未结 3 1159
感情败类
感情败类 2021-01-16 09:45

How can I match two lists of letters without considering the order of letters appearance in that lists in Python

Eg: Think my first list is [\'a\',\'b\',\'c\',

相关标签:
3条回答
  • 2021-01-16 10:19

    You could sort them:

    In [1]: a = list('abcd')
    In [2]: b = list('bcad')
    In [3]: sorted(a) == sorted(b)
    Out[3]: True
    
    In [4]: a == b
    Out[4]: False
    
    0 讨论(0)
  • 2021-01-16 10:30

    I had something different in mind, that is, like this:

    all(x in a for x in b) and all(x in b for x in a)
    

    This checks if all letters in a occur in b, and all letters of b occur in a. This means that they 'match' if a and b are sets.

    But since there was already a good answer, I decided to do a speed comparison, and it turns out my solution is considerably faster than the solution Daren and Lev suggested based on sorted(). For strings with a length under 100 characters, it also outperformed Daren's set(a) == set(b).

    import timeit, random, string
    
    def randstring(length):
        return ''.join(random.choice(string.ascii_lowercase) \
                       for i in xrange(length))
    
    def sortmatch(a,b):
        return sorted(a) == sorted(b)
    
    def bothways(a,b):
        return all(x in a for x in b) and all(x in b for x in a)
    
    def setmatch(a,b):
        return set(a) == set(b)
    
    c1 = "sortmatch(a,b)"
    c2 = "setmatch(a,b)"
    c3 = "bothways(a,b)"
    
    init = """
    from __main__ import randstring, sortmatch, bothways, setmatch
    a = randstring(%i)
    b = randstring(%i)
    """
    
    lengths = [5,20,100,1000,5000]
    times = 10000
    
    for n in lengths:
    
        t1 = timeit.Timer(stmt=c1, setup=init % (n,n))
        t2 = timeit.Timer(stmt=c2, setup=init % (n,n))
        t3 = timeit.Timer(stmt=c3, setup=init % (n,n))
    
        print("String length: %i" % n)
        print("Sort and match:  %.2f" % (t1.timeit(times)))
        print("Set and match:  %.2f" % (t2.timeit(times)))    
        print("Check both ways: %.2f\n" % (t3.timeit(times)))
    

    Results:

    String length: 5
    Sort and match: 0.04
    Set and match: 0.03
    Check both ways: 0.02

    String length: 20
    Sort and match: 0.11
    Set and match: 0.06
    Check both ways: 0.02

    String length: 100
    Sort and match: 0.53
    Set and match: 0.16
    Check both ways: 0.25

    String length: 1000
    Sort and match: 6.86
    Set and match: 0.89
    Check both ways: 3.82

    String length: 5000
    Sort and match: 36.67
    Set and match: 4.28
    Check both ways: 19.49

    0 讨论(0)
  • 2021-01-16 10:34

    How about:

    # if you don't want to consider duplicates either
    output = set(your_first_list) == set(your_second_list)
    
    # if duplicates matter
    output = sorted(your_first_list) == sorted(your_second_list)
    
    0 讨论(0)
提交回复
热议问题