How do you detect adjacent state transitions in one pass?
pairwise emits each item with its immediate successor. A nonempty input of length n gives n−1 pairs.
Python example and interpretation
Compare neighbors in a queued, running, running, done trace to skip the repeated state. Separate jobs before scanning an interleaved log.
from itertools import pairwise
[(a,b) for a,b in pairwise(['queued','run','run','done']) if a!=b]Result [('queued', 'run'), ('run', 'done')]
中文
怎样在单次遍历里找出相邻状态变化?
pairwise 让每个元素与它的直接后继成对出现,长度为 n 的非空输入产生 n−1 对。
Python 算例与适用条件
运行状态依次为排队、运行、运行、完成。比较每一对即可略过中间的重复状态;若日志混合多个作业,先按作业拆开。
from itertools import pairwise
[(a,b) for a,b in pairwise(['queued','run','run','done']) if a!=b]结果 [('queued', 'run'), ('run', 'done')]