问题
I am dealing with lists ls
that look like the following:
ls = ['y4','j8','Le1','Retx3','MNxe4','Xe4','Xf4','Xe5']
Given one such list, I am trying to find out if it contains 3 consecutive elements that match the following pattern:
- 3 consecutive elements starting with the letter
X
, and having the same length (here 3). - And their ending digits may only correspond to either of the following cases: denote the ending digit of the first found element by
n
, then the allowed sequences of the 3 ending digits can be:n, n, n+1
,n, n, n-1
,n, n+1, n
, orn,n-1,n
.
So in the example ls
shown above, we indeed find a matching pattern in the list: 'Xe4', 'Xf4', 'Xe5'
, where all mentioned criteria are validated (with the ending digits being of type n,n,n+1
).
Question
- Are there simple and efficient approaches for such list-based pattern matching purposes inherent to python? Reading similar discussions tells me one should opt for
itertools
capabilities, such as groupby, but I haven't been able to make much progress.
My first intuition and without itertools
, being for the first time subject to such task, is to simply loop over the list elements, and as soon as one of the conditions is met (such as matching starting letters), I pause and inspect the remaining conditions. So in my loop, given iterator i
, I'd be constantly checking for pattern emerging from the elements, i-2, i-1, i
, i-1, i, i+1
, and i, i+1, i+2
. But this has become already a bit messy and filled with conditionals that are hard to sanity check. The point in efficiency lies in the fact that I intend to eventually loop over millions of such lists and look for the pattern. Any insights for how to do this in a manner typified by Python would be very helpful.
来源:https://stackoverflow.com/questions/64106974/finding-consecutive-text-elements-in-list-that-match-pattern