Match two lists of letters in Python

耗尽温柔 提交于 2019-12-01 14:11:54

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)

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!