I have a list with duplicate values ββas shown below:
x = [1, 1, 1, 2, 2, 2, 1, 1, 1]
This list is created from a pattern matching the regular expression (not shown here). The list will have duplicate values ββ(many, many repetitions - hundreds, if not thousands) and will never be randomly ordered, because it is that the regular expression matches every time.
I want to keep track of indexes on a list in which entries change from the previous value . Therefore, for the above list x I want to get a change tracking list [3, 6] indicating that x[3] and x[6] are different from previous entries in the list.
I managed to do this, but I was wondering if there is a cleaner way. Here is my code:
x = [1, 1, 1, 2, 2, 2, 1, 1, 1] flag = [] for index, item in enumerate(x): if index != 0: if x[index] != x[index-1]: flag.append(index) print flag
Output : [3, 6]
Question : Is there a cleaner way to do what I want in fewer lines of code?
prrao source share