通过字符串切片:
def search(string, search_terms):
# Init
ret = []
# Find all terms
# Does not find duplicates, employ count() for that
for term in search_terms:
found = string.find(term)
# Not found
if found < 0:
continue
# Add index of found and length of term
ret.append((found, len(term),))
# Not found
if ret == []:
return [string]
# Sort by index
ret.sort(key=lambda x: x[0])
# Init results list
end = []
# Do first found as it is special
generator = iter(ret)
ind, length = next(generator)
# End index of match
end_index = ind + length
# Add both to results list
end.append(string[:ind])
end.append(string[ind:end_index])
# Do for all other results
for ind, length in generator:
end.append(string[end_index:ind])
end_index = ind + length
end.append(string[ind:end_index])
# Add rest of the string to results
end.append(string[end_index:])
return end
# Initiaze
search_terms = ("standard", "of", "total", "sum")
string = '10-methyl-Hexadecanoic acid of total fatty acids'
print(search(string, search_terms))
# ['10-methyl-Hexadecanoic acid ', 'of', ' ', 'total', ' fatty acids']
如有必要,可以轻松删除空格。