简体   繁体   English

匹配Python中的两个字母列表

[英]Match two lists of letters in Python

How can I match two lists of letters without considering the order of letters appearance in that lists in Python 如何在不考虑Python中该列表中字母出现顺序的情况下匹配两个字母列表

Eg: Think my first list is ['a','b','c','d'] and I want to match this list with another list ['b','c','a','d'] then to get a out put as true. 例如:认为我的第一个列表是['a','b','c','d'] ,我想将此列表与另一个列表['b','c','a','d']然后得出真实的结果。 How to do this? 这个怎么做? I'm new to python and want your help! 我是python新手,需要您的帮助!

Thanks in advance 提前致谢

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 . 此检查,如果在所有字母a发生在b ,和所有字母b出现a This means that they 'match' if a and b are sets. 这意味着, 如果设置 ab ,则它们“匹配”。

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() . 但是由于已经有了一个很好的答案,所以我决定进行速度比较,结果证明我的解决方案比Daren和Lev基于sorted()提出的解决方案要快得多 For strings with a length under 100 characters, it also outperformed Daren's set(a) == set(b) . 对于长度少于100个字符的字符串,它的性能也优于Daren的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 字符串长度:5
Sort and match: 0.04 排序和匹配:0.04
Set and match: 0.03 设置并匹配:0.03
Check both ways: 0.02 双向检查:0.02

String length: 20 字符串长度:20
Sort and match: 0.11 排序和匹配:0.11
Set and match: 0.06 设置并匹配:0.06
Check both ways: 0.02 双向检查:0.02

String length: 100 字符串长度:100
Sort and match: 0.53 排序和匹配:0.53
Set and match: 0.16 设置并匹配:0.16
Check both ways: 0.25 双向检查:0.25

String length: 1000 字串长度:1000
Sort and match: 6.86 排序和匹配:6.86
Set and match: 0.89 设置并匹配:0.89
Check both ways: 3.82 两种方式都检查:3.82

String length: 5000 字符串长度:5000
Sort and match: 36.67 排序和匹配:36.67
Set and match: 4.28 设置并匹配:4.28
Check both ways: 19.49 两种方式都检查:19.49

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM