単一の整数列を含むファイルがあります。このファイルでは、連続して2回同じ数字で始まり、長さが12の整数(重複するサブシーケンスを含む)であるすべての連続サブシーケンス(つまり、連続シーケンスで発生するサブシーケンス)のリストを抽出したいと思います。
また、ファイルの整数以外の行は無視/削除する必要があり、シーケンスが12個の整数に達する前に入力の終わりに達すると、短縮されたシーケンスを連続して出力する必要があります。
たとえば、入力ファイルに次のデータが含まれているとします。
1
junk
1
1
2
3
4
4
5
6
7
8
9
10
11
12
13
14
15
15
16
その後、ソリューションは次の出力を生成する必要があります。
1 1 1 2 3 4 4 5 6 7 8 9
1 1 2 3 4 4 5 6 7 8 9 10
4 4 5 6 7 8 9 10 11 12 13 14
15 15 16
このjunk
行と空の行は無視されるため、最初の3行1
は連続したものと見なされます。
ベストアンサー1
目的のタスクを実行するPythonスクリプトは次のとおりです。
#!/usr/bin/env python2
# -*- coding: ascii -*-
"""extract_subsequences.py"""
import sys
import re
# Open the file
with open(sys.argv[1]) as file_handle:
# Read the data from the file
# Remove white-space and ignore non-integers
numbers = [
line.strip()
for line in file_handle.readlines()
if re.match("^\d+$", line)
]
# Set a lower bound so that we can output multiple lists
lower_bound = 0
while lower_bound < len(numbers)-1:
# Find the "start index" where the same number
# occurs twice at consecutive locations
start_index = -1
for i in range(lower_bound, len(numbers)-1):
if numbers[i] == numbers[i+1]:
start_index = i
break
# If a "start index" is found, print out the two rows
# values and the next 10 rows as well
if start_index >= lower_bound:
upper_bound = min(start_index+12, len(numbers))
print(' '.join(numbers[start_index:upper_bound]))
# Update the lower bound
lower_bound = start_index + 1
# If no "start index" is found then we're done
else:
break
データがというディレクトリにあると仮定すると、data.txt
次のようにこのスクリプトを実行できます。
python extract_subsequences.py data.txt
入力ファイルがdata.txt
次のようになるとします。
1
1
1
2
3
4
5
6
7
8
9
10
11
12
その後、出力は次のようになります。
1 1 1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10 11
出力をファイルに保存するには、出力リダイレクトを使用します。
python extract_subsequences.py data.txt > output.txt