Steven Bethard <[EMAIL PROTECTED]> writes: > Mike C. Fletcher wrote: [...] > > >>> def changes( dataset ): > > ... last = None > > ... for value in dataset: > > ... if value != last: > > ... yield value > > ... last = value > > ... >>> print list(changes(data )) > > which is quite readable/elegant IMO. > > But fails if the list starts with None: > > py> lst = [None,0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5] > py> def changes(dataset): > ... last = None > ... for value in dataset: > ... if value != last: > ... yield value > ... last = value > ... > py> list(changes(lst)) > [0, 1, 2, 3, 2, 4, 5] > > A minor modification that does appear to work: > > py> def changes(dataset): > ... last = object() > ... for value in dataset: > ... if value != last: > ... yield value > ... last = value > ... > py> list(changes(lst)) > [None, 0, 1, 2, 3, 2, 4, 5]
Unless the first object in the list has a weird __cmp__ (does happen...). OK, weird __cmp__s are nasty anyway, but still, why compound it through cleverness when you can write a really plodding function that *always* does what it says on the tin? clever-is-evil-ly y'rs, John -- http://mail.python.org/mailman/listinfo/python-list