检查字符串是否在列表中的任何项目中包含特定字符的最快方法 [英] Fastest way to check if a string contains specific characters in any of the items in a list

查看:156
本文介绍了检查字符串是否在列表中的任何项目中包含特定字符的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查字符串是否包含列表中任何项目中的某些字符的最快方法是什么?

What is the fastest way to check if a string contains some characters from any items of a list?

当前,我正在使用此方法:

Currently, I'm using this method:

lestring = "Text123"

lelist = ["Text", "foo", "bar"]

for x in lelist:
    if lestring.count(x):
        print 'Yep. "%s" contains characters from "%s" item.' % (lestring, x)

有没有没有迭代的方法(我想这会使它更快)?

Is there any way to do it without iteration (which will make it faster I suppose.)?

推荐答案

您可以尝试通过会员资格检查来理解列表

You can try list comprehension with membership check

>>> lestring = "Text123"
>>> lelist = ["Text", "foo", "bar"]
>>> [e for e in lelist if e in lestring]
['Text']

与您的实现相比,尽管LC有一个隐式循环,但是它更快,因为没有像count

Compared to your implementation, though LC has an implicit loop but its faster as there is no explicit function call as in your case with count

与Joe的实现相比,您的实现更快,因为filter函数需要循环调用两个函数lambdacount

Compared to Joe's implementation, yours is way faster, as the filter function would require to call two functions in a loop, lambda and count

>>> def joe(lelist, lestring):
    return ''.join(random.sample(x + 'b'*len(x), len(x)))

>>> def uz(lelist, lestring):
    for x in lelist:
        if lestring.count(x):
            return 'Yep. "%s" contains characters from "%s" item.' % (lestring, x)


>>> def ab(lelist, lestring):
    return [e for e in lelist if e in lestring]

>>> t_ab = timeit.Timer("ab(lelist, lestring)", setup="from __main__ import lelist, lestring, ab")
>>> t_uz = timeit.Timer("uz(lelist, lestring)", setup="from __main__ import lelist, lestring, uz")
>>> t_joe = timeit.Timer("joe(lelist, lestring)", setup="from __main__ import lelist, lestring, joe")
>>> t_ab.timeit(100000)
0.09391469893125759
>>> t_uz.timeit(100000)
0.1528471407273173
>>> t_joe.timeit(100000)
1.4272649857800843


对于较短的字符串,Jamie的评论解决方案比较慢.这是测试结果


Jamie's commented solution is slower for shorter string's. Here is the test result

>>> def jamie(lelist, lestring):
    return next(itertools.chain((e for e in lelist if e in lestring), (None,))) is not None

>>> t_jamie = timeit.Timer("jamie(lelist, lestring)", setup="from __main__ import lelist, lestring, jamie")
>>> t_jamie.timeit(100000)
0.22237164127909637


如果需要布尔值,对于较短的字符串,只需修改上述LC表达式


If you need Boolean values, for shorter strings, just modify the above LC expression

[e in lestring for e in lelist if e in lestring]

或者对于更长的字符串,您可以执行以下操作

Or for longer strings, you can do the following

>>> next(e in lestring for e in lelist if e in lestring)
True

>>> any(e in lestring for e in lelist)

这篇关于检查字符串是否在列表中的任何项目中包含特定字符的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆