我需要在文本文件中找到给定的模式并打印匹配的模式。文本文件是一串数字,模式可以是任何数字串或由“X”表示的占位符。
我想解决这个问题的方法是将序列加载到一个变量中,然后创建一个可测试的子序列列表,然后测试每个子序列。这是我在 python 中的第一个函数,所以我对如何轻松创建测试序列列表然后对其进行测试感到困惑。
def find(pattern): #finds a pattern in the given input file
with open('sequence.txt', 'r') as myfile:
string = myfile.read()
print('Test data is:', string)
testableStrings = []
#how to create a list of testable sequences?
for x in testableStrings:
if x == pattern:
print(x)
return
例如,在“11012102”中搜索“X10X”应打印“1101”和“2102”。
森林海
相关分类