Python 从不带标点符号的字符串搜索到主字符串,然后从主字符串中提取带标点符号但不带库的片段,可能吗?

Python 从不带标点符号的字符串搜索到主字符串,然后从主字符串中提取带标点符号但不带库的片段,可能吗?,python,string,slice,Python,String,Slice,我要做的作业是图书馆不允许做的,我低估了这个问题: 假设我们有一个字符串列表:str_list=[我的头,终于自由了,进入警报状态,再过一会儿,脖子] 关于这一点,我们可以肯定的是,每一个字符串都包含在master_字符串中,它们是有序的,并且没有标点符号。所有这些都要感谢我以前的控制 然后我们有了字符串:master_string=‘来吧,我的头终于自由了!’爱丽丝用一种高兴的口吻说,过了一会儿,这种口吻变成了惊慌,因为她发现自己的肩膀不见了。她往下看,只看到一条长长的脖子,它好像从她下面一片

我要做的作业是图书馆不允许做的,我低估了这个问题:

假设我们有一个字符串列表:str_list=[我的头,终于自由了,进入警报状态,再过一会儿,脖子]

关于这一点,我们可以肯定的是,每一个字符串都包含在master_字符串中,它们是有序的,并且没有标点符号。所有这些都要感谢我以前的控制

然后我们有了字符串:master_string=‘来吧,我的头终于自由了!’爱丽丝用一种高兴的口吻说,过了一会儿,这种口吻变成了惊慌,因为她发现自己的肩膀不见了。她往下看,只看到一条长长的脖子,它好像从她下面一片绿叶中拔出一根麦秆

这里我要做的基本上是检查主字符串中包含的str_列表中至少k的字符串序列,在这种情况下k=2,然而,我低估了一个事实,即在str_列表中,我们每个字符串中有超过1个单词,因此执行master_string.split不会带我去任何地方,因为这意味着询问我的头是否=我的,这当然是错误的

我在考虑做一些事情,比如一次连接一个字符串,然后搜索master_string.strip.,:;!?但是如果我找到了相应的序列,我绝对需要直接从master_字符串中获取它们,因为我需要结果变量中的标点符号。这基本上意味着直接从master_字符串中获取切片,但这怎么可能呢?有可能吗,或者我必须改变方法?这让我发疯,尤其是因为没有图书馆可以这么做

如果您想知道这里的预期结果是什么:


[我的头终于自由了!在另一个时刻发出警报,]因为两者都尊重stru列表中至少k个字符串的条件,neck将保存在丢弃列表中,因为它不尊重不能丢弃的条件。pop因为我需要用丢弃的变量做其他事情

我有两个不同的版本,数字1给你脖子:,但数字2没有给你那么多,这是数字1:

master_string = "Come, my head’s free at last!’ said Alice in a tone of delight, which changed into alarm in another moment, when she found that her shoulders were nowhere to be found: all she could see, when she looked down, was an immense length of neck, which seemed to rise like a stalk out of a sea of green leaves that lay far below her."

str_list = ["my head's", "free", "at last", "into alarm", "in another moment", "neck"]

new_str = ''

for word in str_list:
    if word in master_string:
       new_str += word + ' '
            

print(new_str)
这是第二条:

master_string = "Come, my head’s free at last!’ said Alice in a tone of delight, which changed into alarm in another moment, when she found that her shoulders were nowhere to be found: all she could see, when she looked down, was an immense length of neck, which seemed to rise like a stalk out of a sea of green leaves that lay far below her."

str_list = ["my head's", "free", "at last", "into alarm", "in another moment", "neck"]

new_str = ''

for word in str_list:
    if word in master_string:
        new_word = word.split(' ')
        if len(new_word) == 2:
            new_str += word + ' '
            

print(new_str)
遵循我的解决方案:

尝试根据master_字符串和一组有限的标点字符扩展所有代码,例如my head's->my head's free at last!;自由->终于自由了!。 仅保留已扩展至少k次的子字符串。 删除多余的子字符串,例如“最终释放”!现在我的头终于自由了!。 代码如下:

str_list = ["my head’s", "free", "at last", "into alarm", "in another moment", "neck"]
master_string = "‘Come, my head’s free at last!’ said Alice in a tone of delight, which changed into alarm in another moment, when she found that her shoulders were nowhere to be found: all she could see, when she looked down, was an immense length of neck, which seemed to rise like a stalk out of a sea of green leaves that lay far below her."
punctuation_characters = ".,:;!?"  # list of punctuation characters
k = 1

def extend_string(current_str, successors_num = 0) :
    # check if the next token is a punctuation mark
    for punctuation_mark in punctuation_characters :
        if current_str + punctuation_mark in master_string :
            return extend_string(current_str + punctuation_mark, successors_num)
    
    # check if the next token is a proper successor
    for successor in str_list :
        if current_str + " " + successor in master_string :
            return extend_string(current_str + " " + successor, successors_num+1)
    
    # cannot extend the string anymore
    return current_str, successors_num

extended_strings = []
for s in str_list :
    extended_string, successors_num = extend_string(s)
    if successors_num >= k : extended_strings.append(extended_string)

extended_strings.sort(key=len)  # sorting by ascending length
result_list = []
for es in extended_strings :
    result_list = list(filter(lambda s2 : s2 not in es, result_list))
    result_list.append(es)
print(result_list)      # result: ['my head’s free at last!', 'into alarm in another moment,']

所以neck被排除在预期输出之外,因为它前面没有其他字符串,所以k=1;是吗?问题:str_list的字符串只有在与空格连接时才能出现在master_字符串中?是的,确切地说,neck只有一个字符串,因此不符合在前后至少有一个字符串的要求。关于str_列表的字符串是的,它们可以在master_字符串中显示,但它们之间有空格,但缺少标点。那么顶点呢?在str_列表中,我们有我的头,但它与master_字符串的我的头不匹配。我们可以替换str_列表中的所有'with'吗?这只是一个例子,无论如何我不明白你的意思,它们是同一个字符串不?这可能有效,但我无法导入库来解决这个问题,这是违反规则的rules@Krist我用更好的解决方案更新了答案谢谢!现在我要检查一下,我想了解如何将它集成到我的代码中。既然k是一个可以从一次代码运行到另一次代码运行的值,那么这会起作用吗?@Krist I将代码概括为任何可能的k。现在,你将用k=1获得['my head's free!'','into alarm in Other片刻,'],用k=2获得['my head's free at last!']。