如何在固定字符串附近查找匹配项

Pro Q

我正在寻求帮助,以查找允许我获取字符串列表(例如["I like ", " and ", " because "]和单个目标字符串,例如)的Python函数"I like lettuce and carrots and onions because I do",并找到可以对目标字符串中的字符进行分组的所有方式,以便每个字符串列表中的顺序。

例如:

solution(["I like ", " and ", " because ", "do"],
         "I like lettuce and carrots and onions because I do")

应该返回:

[("I like ", "lettuce", " and ", "carrots and onions", " because ", "I ", "do"), 
 ("I like ", "lettuce and carrots", " and ", "onions", " because ", "I ", "do")]

请注意,在每个元组中,list参数中的字符串都是按顺序排列的,并且该函数返回每种可能的方法来拆分目标字符串以实现此目的。

另一个例子,这次只有一种可能的组织字符的方式:

solution(["take ", " to the park"], "take Alice to the park")

应该给出结果:

[("take ", "Alice", " to the park")]

这是一个无法正确组织字符的示例:

solution(["I like ", " because ", ""],
         "I don't like cheese because I'm lactose-intolerant")

应该回馈:

[]

因为没有办法做到。请注意,"I like "第一个参数中的不能拆分。目标字符串中没有字符串"I like ",因此无法匹配。

这是最后一个示例,同样具有多个选项:

solution(["I", "want", "or", "done"],
         "I want my sandwich or I want my pizza or salad done")

应该回来

[("I", " ", "want", " my sandwich ", "or", " I want my pizza or salad ", "done"),
 ("I", " ", "want", " my sandwich or I want my pizza ", "or", " salad ", "done"),
 ("I", " want my sandwich or I", "want", " my pizza ", "or", " salad ", "done")]`

再次注意,每个字符串["I", "want", "or", "done"]按顺序包含在每个元组中,并且其余字符以任何可能的方式围绕这些字符串重新排序。返回所有可能的重新排序列表。

请注意,还假定列表中的第一个字符串将出现在目标字符串的开头,列表中的最后一个字符串将出现在目标字符串的结尾。(如果没有,该函数应返回一个空列表。)

哪些Python函数将允许我执行此操作?

我已经尝试过使用正则表达式函数,但是在有多个选项的情况下,它似乎失败了。

tz

我有一个解决方案,它需要大量的重构,但它似乎可以工作,我希望这会有所帮助,这是一个非常有趣的问题。

import itertools
import re
from collections import deque


def solution(search_words, search_string):
    found = deque()
    for search_word in search_words:
        found.append([(m.start()) for m in re.compile(search_word).finditer(search_string)])
    if len(found) != len(search_words) or len(found) == 0:
        return []  # no search words or not all words found
    word_positions_lst = [list(i) for i in itertools.product(*found) if sorted(list(i)) == list(i)]

    ret_lst = []
    for word_positions in word_positions_lst:
        split_positions = list(itertools.chain.from_iterable(
            (split_position, split_position + len(search_word))
            for split_position, search_word in zip(word_positions, search_words)))
        last_seach_word = search_string[split_positions[-1]:]
        ret_strs = [search_string[a:b] for a, b in zip(split_positions, split_positions[1:])]
        if last_seach_word:
            ret_strs.append(last_seach_word)
        if len(search_string) == sum(map(len,ret_strs)):
            ret_lst.append(tuple(ret_strs))
    return ret_lst


print(solution(["I like ", " and ", " because ", "do"],
               "I like lettuce and carrots and onions because I do"))
print([("I like ", "lettuce", " and ", "carrots and onions", " because ", "I ", "do"),
       ("I like ", "lettuce and carrots", " and ", "onions", " because ", "I ", "do")])
print()

print(solution(["take ", " to the park"], "take Alice to the park"))
print([("take ", "Alice", " to the park")])
print()

print(solution(["I like ", " because "],
               "I don't like cheese because I'm lactose-intolerant"))
print([])
print()

输出:

[('I like ', 'lettuce', ' and ', 'carrots and onions', ' because ', 'I ', 'do'), ('I like ', 'lettuce and carrots', ' and ', 'onions', ' because ', 'I ', 'do')]
[('I like ', 'lettuce', ' and ', 'carrots and onions', ' because ', 'I ', 'do'), ('I like ', 'lettuce and carrots', ' and ', 'onions', ' because ', 'I ', 'do')]

[('take ', 'Alice', ' to the park')]
[('take ', 'Alice', ' to the park')]

[]
[]

[('I', ' ', 'want', ' my sandwich ', 'or', ' I want my pizza or salad ', 'done'), ('I', ' ', 'want', ' my sandwich or I want my pizza ', 'or', ' salad ', 'done'), ('I', ' want my sandwich or I ', 'want', ' my pizza ', 'or', ' salad ', 'done')]
[('I', ' ', 'want', ' my sandwich ', 'or', ' I want my pizza or salad ', 'done'), ('I', ' ', 'want', ' my sandwich or I want my pizza ', 'or', ' salad ', 'done'), ('I', ' want my sandwich or I', 'want', ' my pizza ', 'or', ' salad ', 'done')]

编辑:重构代码以具有有意义的变量名。

Edit2:添加了我忘记的最后一种情况。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Parsec如何在字符串中查找“匹配项”

如果模式是字符串字符,如何查找匹配项?

java - 如何在两个数组中查找字符串之间的匹配项

如何在字符串中查找重复项?

如何在C ++中的长字符串中查找完全匹配的字符串

查找字符串中最准确的匹配项

查找字符串中的部分匹配项

在列中的字符串中查找匹配项

查找字符串中的查询匹配项

查找其键与子字符串匹配的字典项

查找字符串中的所有匹配项

如何查找在正则表达式中不包含字符串的匹配项

如何从向量中查找字符串的所有最长匹配项

如何使用python Regex查找给定字符串中的所有完全匹配项

如何在静态文本附近获取特定字符串?

在C ++ 11中,如何在以给定字符串开头的字符串向量中查找并返回所有项?

Excel 2016:如何在另一列的单元格列中查找所有超字符串匹配项?

在 R 中使用模式匹配,如何查找每个字符串是否有多个匹配项?

如何在字符串数组中查找ArrayList项的索引

如何在 Python 中使用正则表达式查找匹配字符串到特定字符串

在提取匹配项时如何在Ruby中验证字符串的格式?

如何在包含多个匹配项的字符串包含中合并大熊猫?

Vue.js - 找到匹配项时如何在字符串的开头添加 html 标记

如何在字符串中找到所有匹配项

您如何在Chrome中的页面上搜索短语?(不是字符串匹配项)

在javascript中,如何在数组中搜索子字符串匹配项

如何在字符串中找到为特定捕获组找到匹配项的位置?

如何在对象数组中搜索包含字符串的所有匹配项?

如何在VBA中搜索整个单词而不是字符串中的部分匹配项