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

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

问题描述

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

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的实现相比,你的实现更快,因为过滤器函数需要在循环中调用两个函数, lambda count

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)

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

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