python-2.7 - 防止 Python 中的滑动窗口在使用 islice 时跳过范围
问题描述
使用 islice 实现每张幻灯片按 10 个索引移动 10 个样本的滑动窗口时遇到问题。
我的文本文件(input.txt)在每一行“一、二、三……”上都有数字值,以便于识别行索引。
这是我正在使用的代码:
with open("input.txt", "r") as text_file:
for n in range (0,29,10):
window_string = ''
x = n
for line in itertools.islice(text_file, x, x+10):
window_string = window_string + line
print str(x) + " to " + str(x+10)
Stream=window_string.replace('\n', ' ')
print Stream +'\n'
这就是正在输出的内容:
0 to 10
zero one two three four five six seven eight nine
10 to 20
twenty twenty-one twenty-two twenty-three twenty-four twenty-five twenty-six twenty-seven twenty-eight twenty-nine
20 to 30
而不是预期的:
0 to 10
zero one two three four five six seven eight nine
10 to 20
ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
20 to 30
twenty twenty-one twenty-two twenty-three twenty-four twenty-five twenty-six twenty-seven twenty-eight twenty-nine
请让我知道迭代 n 的问题是什么。
解决方案
一直没搞清楚上面的问题是什么。决定改为将文件读入列表,然后在列表上实现滑动窗口。这是我最终使用的代码
lines = [line.rstrip('\n') for line in open('twitter-out2.txt')]
#print lines
for n in range (0,29,10):
streamer = ' '.join(lines[n:n+10])
print "from " + str(n) + " to " + str(n+10)
print streamer
输出如预期:
from 0 to 10
zero one two three four five six seven eight nine
from 10 to 20
ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
from 20 to 30
twenty twenty-one twenty-two twenty-three twenty-four twenty-five twenty-six twenty-seven twenty-eight twenty-nine