Search for a pattern in a list of strings - Python
- by Holtz
I have a list of strings containing filenames such as,
file_names = ['filei.txt','filej.txt','filek.txt','file2i.txt','file2j.txt','file2k.txt','file3i.txt','file3j.txt','file3k.txt']
I then remove the .txt extension using:
extension = os.path.commonprefix([n[::-1] for n in file_names])[::-1]
file_names_strip = [n[:-len(extension)] for n in file_names]
And then return the last character of each string in the list file_names_strip:
h = [n[-1:] for n in file_names_strip]
Which gives h = ['i', 'j', 'k', 'i', 'j', 'k', 'i', 'j', 'k']
How can i test for a pattern of strings in h? So if i,j,k occur sequentially it would return True and False if not. I need to know this because not all file names are formatted like they are in file_names.
So:
test_ijk_pattern(h) = True
no_pattern = ['1','2','3','1','2','3','1','2','3']
test_ijk_pattern(no_pattern) = False