【问题标题】:Text split into paragraphs and then into strings文本拆分为段落,然后拆分为字符串
【发布时间】:2020-07-27 16:33:29
【问题描述】:

我正在使用以下代码但出现错误

import re
text = 'some big text'
sentences = re.split('some splitter',text)
nsentences=len(sentences)

substring=[]


for i in range(nsentences):
    substring.append[i]=re.split('some splitter',sentences[i]) #error line
    print(substring[i])

TypeError: 'builtin_function_or_method' 对象不支持项目分配

【问题讨论】:

标签: python


【解决方案1】:

您收到的错误是因为list.append() 的分配不正确。 您可以通过两种方式在substring 中执行re.split('some splitter', sentences[i]) 的追加/插入:

追加:

for s in sentences:
    substring.append(re.split('some splitter', s))

插入:

for i in range(nsentences):
    substring.insert(i, re.split('some splitter', sentences[i]))

供参考: https://docs.python.org/3/tutorial/datastructures.html

【讨论】:

  • 或者最好使用理解:substring = [re.split('some splitter', s) for s in sentences]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多