【问题标题】:Split a string according to the sentence phrases contained in a list in Python根据Python中列表中包含的句子短语拆分字符串
【发布时间】:2021-07-25 08:58:14
【问题描述】:

我想根据与列表重合的短语来拆分一个句子。 例如:

sentence = "the world is too big"
list = ["too big", "too small", "the world", "too many"]

如果句子中包含属于列表的词组,它想看到的分解结果是:

result = ["the world", "is", "too big"]

代替:

result = ["the", "world", "is", "too", "big"]

非常感谢

【问题讨论】:

  • 只是一个旁注,永远不要使用list这个词来定义变量,因为你正在用它来隐藏内置的listdictstrint 等也是如此

标签: python list


【解决方案1】:
sentence = "the world is too big"
list = ["too big", "too small", "the world", "too many"]
for data in list:
  if data in sentence:
   sentence = sentence.replace(data, data.replace(' ', '..............'))
sentence = sentence.split(' ')
for i in range(len(sentence)):
 if '..............' in sentence[i]:
  sentence[i] = sentence[i].replace('..............', ' ')
print(sentence)

【讨论】:

    【解决方案2】:

    使用re.split:

    import re
    
    s = "the world is too big"
    l = ["too big", "too small", "the world", "too many"]
    r = fr"\s*({'|'.join(l)})\s*"
    
    >>> re.split(r, s)[1:-1]
    ['the world', 'is', 'too big']
    
    >>> r
    '\\s*(too big|too small|the world|too many)\\s*'
    

    【讨论】:

    • 非常感谢您的回答,但是我换了一个句子后,显示的结果并不是列表中除了短语之外的所有单词都被一一分解。例如:当s = "我认为世界不是太大"时,希望结果是['I','think','the world','is','not','too big'] , not ['世界', '不是', '太大']。
    猜你喜欢
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2013-01-24
    • 2021-07-08
    • 2023-04-03
    相关资源
    最近更新 更多