Python有string.find()和string.rfind()来获取字符串中子字符串的索引。
我想知道是否有像string.find_all()这样的东西可以返回所有找到的索引(不仅是从开始的第一个索引,还是从结束的第一个索引)。
例如:
string = "test test test test"
print string.find('test') # 0
print string.rfind('test') # 15
#this is the goal
print string.find_all('test') # [0,5,10,15]
要统计出现次数,请参见计算字符串中子字符串出现的次数。
没有简单的内置字符串函数来做你正在寻找的事情,但你可以使用更强大的正则表达式:
import re
[m.start() for m in re.finditer('test', 'test test test test')]
#[0, 5, 10, 15]
如果你想找到重叠的匹配,lookahead会这样做:
[m.start() for m in re.finditer('(?=tt)', 'ttt')]
#[0, 1]
如果你想要一个没有重叠的反向查找-all,你可以将正负前向组合成这样的表达式:
search = 'tt'
[m.start() for m in re.finditer('(?=%s)(?!.{1,%d}%s)' % (search, len(search)-1, search), 'ttt')]
#[1]
red .finditer返回一个生成器,因此您可以将上面的[]更改为()以获得一个生成器,而不是一个列表,如果您只迭代一次结果,这将更有效。
对于非重叠匹配,可以使用re.finditer()。
>>> import re
>>> aString = 'this is a string where the substring "is" is repeated several times'
>>> print [(a.start(), a.end()) for a in list(re.finditer('is', aString))]
[(2, 4), (5, 7), (38, 40), (42, 44)]
但不适用于:
In [1]: aString="ababa"
In [2]: print [(a.start(), a.end()) for a in list(re.finditer('aba', aString))]
Output: [(0, 3)]
>>> help(str.find)
Help on method_descriptor:
find(...)
S.find(sub [,start [,end]]) -> int
因此,我们可以自己构建它:
def find_all(a_str, sub):
start = 0
while True:
start = a_str.find(sub, start)
if start == -1: return
yield start
start += len(sub) # use start += 1 to find overlapping matches
list(find_all('spam spam spam spam', 'spam')) # [0, 5, 10, 15]
不需要临时字符串或正则表达式。
来,让我们一起递归。
def locations_of_substring(string, substring):
"""Return a list of locations of a substring."""
substring_length = len(substring)
def recurse(locations_found, start):
location = string.find(substring, start)
if location != -1:
return recurse(locations_found + [location], location+substring_length)
else:
return locations_found
return recurse([], 0)
print(locations_of_substring('this is a test for finding this and this', 'this'))
# prints [0, 27, 36]
不需要这样使用正则表达式。
同样,旧线程,但这里是我的解决方案使用生成器和普通str.find。
def findall(p, s):
'''Yields all the positions of
the pattern p in the string s.'''
i = s.find(p)
while i != -1:
yield i
i = s.find(p, i+1)
例子
x = 'banananassantana'
[(i, x[i:i+2]) for i in findall('na', x)]
返回
[(2, 'na'), (4, 'na'), (6, 'na'), (14, 'na')]
其他人提供的解决方案完全基于可用的find()方法或任何可用的方法。
找出a的所有出现点的核心基本算法是什么
字符串中的子字符串?
def find_all(string,substring):
"""
Function: Returning all the index of substring in a string
Arguments: String and the search string
Return:Returning a list
"""
length = len(substring)
c=0
indexes = []
while c < len(string):
if string[c:c+length] == substring:
indexes.append(c)
c=c+1
return indexes
你也可以继承str类到新的类,并可以使用这个函数
在下面。
class newstr(str):
def find_all(string,substring):
"""
Function: Returning all the index of substring in a string
Arguments: String and the search string
Return:Returning a list
"""
length = len(substring)
c=0
indexes = []
while c < len(string):
if string[c:c+length] == substring:
indexes.append(c)
c=c+1
return indexes
调用方法
newstr。find_all('你觉得这个答案有用吗?然后upvote
这个!”、“这”)
python的方法是:
mystring = 'Hello World, this should work!'
find_all = lambda c,s: [x for x in range(c.find(s), len(c)) if c[x] == s]
# s represents the search string
# c represents the character string
find_all(mystring,'o') # will return all positions of 'o'
[4, 7, 20, 26]
>>>
当在一份文件中寻找大量的关键词时,使用flash文本
from flashtext import KeywordProcessor
words = ['test', 'exam', 'quiz']
txt = 'this is a test'
kwp = KeywordProcessor()
kwp.add_keywords_from_list(words)
result = kwp.extract_keywords(txt, span_info=True)
在大量搜索词列表上,Flashtext比正则表达式运行得更快。
这个函数不会查看字符串内的所有位置,它不会浪费计算资源。我的尝试:
def findAll(string,word):
all_positions=[]
next_pos=-1
while True:
next_pos=string.find(word,next_pos+1)
if(next_pos<0):
break
all_positions.append(next_pos)
return all_positions
要使用它,可以这样调用它:
result=findAll('this word is a big word man how many words are there?','word')
这是来自hackerrank的一个类似问题的解决方案。我希望这能帮助到你。
import re
a = input()
b = input()
if b not in a:
print((-1,-1))
else:
#create two list as
start_indc = [m.start() for m in re.finditer('(?=' + b + ')', a)]
for i in range(len(start_indc)):
print((start_indc[i], start_indc[i]+len(b)-1))
输出:
aaadaa
aa
(0, 1)
(1, 2)
(4, 5)
这不完全是OP要求的,但你也可以使用split函数来获得所有子字符串不出现的列表。OP没有指定代码的最终目标,但如果您的目标是删除子字符串,那么这可能是一个简单的一行程序。对于更大的字符串,可能有更有效的方法来做到这一点;在这种情况下,正则表达式更可取
# Extract all non-substrings
s = "an-example-string"
s_no_dash = s.split('-')
# >>> s_no_dash
# ['an', 'example', 'string']
# Or extract and join them into a sentence
s_no_dash2 = ' '.join(s.split('-'))
# >>> s_no_dash2
# 'an example string'
我简单浏览了一下其他的答案,如果这个已经在上面了,我很抱歉。
试试这个,对我有用!
x=input('enter the string')
y=input('enter the substring')
z,r=x.find(y),x.rfind(y)
while z!=r:
print(z,r,end=' ')
z=z+len(y)
r=r-len(y)
z,r=x.find(y,z,r),x.rfind(y,z,r)