python itertools与islice错误

时间:2016-06-29 22:19:47

标签: python

我还在学习python,我有下面的代码,但它不起作用:

from itertools import *

startword = ["start",]
stopword = ["stop",]
text = "this is a text that starts with some test stuff and then after that it stop right here!"

for i in islice(text.split(" "), startword, stopword):
    print i

我试图在开始和停止之间打印单词,而不知道它们之间有多少单词。 从错误我看起来我需要一个整数来启动和停止islice的参数。这是错误&#34; ValueError: Indices for islice() must be None or an integer: 0 <= x <= maxing.&#34; 我可以使用的任何其他itertool?!

谢谢,

2 个答案:

答案 0 :(得分:1)

在这种特殊情况下,islice是一个糟糕的选择;你在一个已实现的序列中拥有内存中的所有数据,因此islice只是浪费时间迭代前导值。有很多更好的方法来处理这个问题,要么使用index来查找startend索引并执行真正的切片,要么更聪明,并通过拆分来减少工作量start/end分隔部分,仅拆分该部分以提取单词。例如:

text = "this is a text that starts with some test stuff and then after that it stop right here!"

_, text = text.split('start', 1)  # Remove start and stuff before it
text, _ = text.rsplit('stop', 1)  # Remove stop and stuff after it

for word in text.split():  # Split what remains on whitespace
    print word

请注意,这仍然不是正确的(你的有界区域以&#34开始&#34;不是&#34;开始&#34;,所以你最终得到一个领先的&#34; &#34; s&#34;),但是可以使用适当的边界和通配符切换到re.split以适合您的方案的方式修复它。

答案 1 :(得分:0)

您需要计算&#39; start&#39;的索引。并且&#39;停止&#39;然后你提供     两个整数值,而不是字符串:

for i in islice(text.split(" "), startword_index, stopword_index):
    print i
相关问题