【问题标题】:From strings without punctuation search into master string and take from there slices with punctuation without libraries, possible?从没有标点符号的字符串搜索到主字符串并从那里获取没有库的标点符号切片,可能吗?
【发布时间】:2021-05-03 08:09:24
【问题描述】:

我有这个功课要做(不允许图书馆),我低估了这个问题:

假设我们有一个字符串列表:str_list = ["my head's", "free", "at last", "into alarm", "in another moment", "neck"]

我们可以肯定的是,每个字符串都包含在 master_string 中,都是有序的,并且没有标点符号。 (这一切都归功于我之前所做的控制)

然后我们得到字符串: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."

我在这里必须做的基本上是检查包含在 master_string 中的 str_list 中至少 k 的字符串序列(在本例中为 k = 2),但是我低估了在 str_list 中我们有超过 1 个单词的事实在每个字符串中,这样做 master_string.split() 不会带我去任何地方,因为这意味着要问像 if "my head's" == "my" 这样的问题,这当然是错误的。

我当时正在考虑以某种方式连接字符串并搜索master_string.strip(".,:;!?"),但如果我找到相应的序列,我绝对需要直接从master_string中获取它们,因为我需要结果变量中的标点符号。这基本上意味着直接从 master_string 中获取切片,但这怎么可能呢?甚至有可能还是我必须改变方法?这让我完全发疯,特别是因为没有允许这样做的库。

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

["my head's free at last!", "into alarm in another moment,"](因为两者都尊重 str_list 中至少 k 个字符串的条件)和“neck”将被保存在discard_list 中,因为它不遵守该条件(不能用 .pop() 丢弃它因为我需要在丢弃变量的情况下做其他事情)

【问题讨论】:

  • 所以neck被排除在你的预期输出之外,因为前面没有其他字符串,所以k=1;对吗?
  • 问题:str_list 的字符串只有在与空格连接时才能出现在master_string 中?
  • 是的,“neck”只有 1 个字符串,因此不遵守之前或之后至少 1 个字符串的要求。关于 str_list 的字符串,是的,它们可以在 master_string 中显示,它们之间有空格,但缺少标点符号。
  • 那么顶点呢?在str_list 我们有my head's,但它与master_stringmy head’s 不匹配。我们可以将str_list 中的所有' 替换为 吗?
  • 这只是一个例子,反正我没明白你的意思,它们是同一个字符串吗?

标签: python string slice


【解决方案1】:

按照我的解决方案:

  1. 尝试根据master_string 和一组有限的标点符号(例如my head’s -> my head’s free at last!free -> free at last!)扩展所有内容。
  2. 仅保留已扩展至少k 次的子字符串。
  3. 删除多余的子字符串(例如,free at last! 已经存在于 my head’s free at last!)。

这是代码:

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,']

【讨论】:

  • 这本可以工作,但我无法导入库来解决这个问题,违反了规则
  • @Krist 我用更好的解决方案更新了答案
  • 谢谢!现在去检查一下
  • 我想了解如何将它集成到我的代码中。由于“k”是一个可以从代码运行更改为另一个值的值,这是否仍然有效?
  • @Krist 我将代码概括为任何可能的k。现在您将获得['my head’s free at last!', 'into alarm in another moment,']k=1['my head’s free at last!']k=2
【解决方案2】:

我有两个不同的版本,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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多