list=['a','a','x','c','e','e','f','f','f']
i=0
count = 0
while count < len(list)-2:
if list[i] == list[i+1]:
if list [i+1] != list [i+2]:
print list[i]
i+=1
count +=1
else:print "no"
else:
i +=1
count += 1
I'm getting:
else:print "no"
^
IndentationError: unexpected indent
I'm trying to only print the elts that match the following element, but not the element following that. I'm new to Python, and I'm not sure why this isn't working.
Here is the fixed-up code (added a count += 1
after the else-clause to make sure it terminates):
list=['a','a','x','c','e','e','f','f','f']
i=0
count = 0
while count < len(list)-2:
if list[i] == list[i+1]:
if list [i+1] != list [i+2]:
print list[i]
i+=1
count +=1
else:
print "no"
count += 1
else:
i +=1
count += 1
A more advanced solution using itertools is more compact and easier to get right:
from itertools import groupby
data = ['a','a','x','c','e','e','f','f','f']
for k, g in groupby(data):
if len(list(g)) > 1:
print k
The code works for me without error (although you get stuck in a loop). Make sure you are not mixing tabs and spaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With