Conditional loop in python -
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: +=1 count += 1
i'm getting:
else:print "no" ^ indentationerror: unexpected indent
i'm trying print elts match following element, not element following that. i'm new python, , i'm not sure why isn't working.
here fixed-up code (added count += 1
after else-clause make sure 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: +=1 count += 1
a more advanced solution using itertools more compact , easier right:
from itertools import groupby data = ['a','a','x','c','e','e','f','f','f'] k, g in groupby(data): if len(list(g)) > 1: print k
Comments
Post a Comment