我的目的是提取子字符串并将它们扩展到大字符串中的下两个单词。下面是字符串,索引列表和输出,以提供清晰度。
示例:
>>> _string='the old school teacher is having a nice time at school'
>>> index_list=[[0,8],[23,35]]
>>> [_string[x[0]:x[-1]] for x in index_list]
Output:>>> ['the old s', 'is having a n']
我的目标不是扩展子字符串以覆盖接下来的两个单词。子字符串的最后一个字符应该扩展到教师和时间。
期望的输出:
['the old school teacher', 'is having a nice time']
如果你需要更多解释,请告诉我。
有什么建议吗?
答案 0 :(得分:2)
这是一个很简单的方法......
>>> def tiger(inval, start, end):
... base = list(inval[start: end])
... spaces = 0
... while spaces < 2 and end < len(inval):
... char = inval[end]
... if char == " ":
... spaces += 1
... base.append(char)
... end += 1
... return "".join(base).strip()
...
>>> tiger(_string, 0, 8)
'the old school teacher'
>>> tiger(_string, 23, 35)
'is having a ice time'
>>> tiger(_string, 45, 85)
'at school'
这假设您总是假设要在空格上分割单词(不是标点符号 - 尽管简单的正则表达式或字符集可以解决这个问题)。