python集合查找元素_Python3快速查找集合中的任何元素是字符串的子串

如果我有一个字符串集合,那么有一个数据结构或函数可以提高检查集合中的任何元素是否是我的主字符串上的子字符串的速度?

现在我循环遍历我的字符串数组并使用in运算符.有更快的方法吗?

import timing

## string match in first do_not_scan

## 0:00:00.029332

## string not in do_not_scan

## 0:00:00.035179

def check_if_substring():

for x in do_not_scan:

if x in string:

return True

return False

## string match in first do_not_scan

## 0:00:00.046530

## string not in do_not_scan

## 0:00:00.067439

def index_of():

for x in do_not_scan:

try:

string.index(x)

return True

except:

return False

## string match in first do_not_scan

## 0:00:00.047654

## string not in do_not_scan

## 0:00:00.070596

def find_def():

for x in do_not_scan:

if string.find(x) != -1:

return True

return False

string = '/usr/documents/apps/components/login'

do_not_scan = ['node_modules','bower_components']

for x in range(100000):

find_def()

index_of()

check_if_substring()

解决方法:

不,没有更快的内置方式.

使用内置方法,最糟糕的情况是:没有匹配,这意味着您已经测试了列表中的每个项目以及每个项目中的几乎每个偏移量.

幸运的是,in运算符非常快(至少在CPython中)并且在我的测试中速度快了近三倍:

0.3364804992452264 # substring()

0.867534976452589 # any_substring()

0.8401796016842127 # find_def()

0.9342398950830102 # index_of()

2.7920695478096604 # re implementation

这是我用于测试的脚本:

from timeit import timeit

import re

def substring():

for x in do_not_scan:

if x in string:

return True

return False

def any_substring():

return any(x in string for x in do_not_scan)

def find_def():

for x in do_not_scan:

if string.find(x) != -1:

return True

return False

def index_of():

for x in do_not_scan:

try:

string.index(x)

return True

except:

return False

def re_match():

for x in do_not_scan:

if re.search(string, x):

return True

return False

string = 'a'

do_not_scan = ['node_modules','bower_components']

print(timeit('substring()', setup='from __main__ import substring'))

print(timeit('any_substring()', setup='from __main__ import any_substring'))

print(timeit('find_def()', setup='from __main__ import find_def'))

print(timeit('index_of()', setup='from __main__ import index_of'))

print(timeit('re_match()', setup='from __main__ import re_match'))

标签:python,python-3-x,algorithm,big-o,string-algorithm

来源: https://codeday.me/bug/20190724/1525657.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值