jeudi 26 septembre 2019

Check that python list follows a specific repeating pattern

I have a python list which contains only two symbols, let us say they are a and b and the list looks like so:

L = ['a','b','a','b','a','b','a','b','a','b','a','b','a','b','a','b']

Now in my application, I have thousands of these lists, and they vary in length (typically a few hundred long). But what they all have in common is that they have the repeating pattern (a,b).

I should say that they all should have that pattern. I am looking for an efficient way, a test, that can determine that each list does in fact have this repeating pattern. And if not throw an error or something e.g.

assert my_fancy_test(L), 'the list does not follow the correct pattern'

I think I am looking for sub-sequence matching, but my google searches are coming up short.

Aucun commentaire:

Enregistrer un commentaire