John Salerno wrote: > Michael Spencer wrote: > >> itertools.groupby makes this very straightforward: > > I was considering this function, but then it seemed like it was only > used for determing consecutive numbers like 1, 2, 3 -- not consecutive > equivalent numbers like 1, 1, 1. But is that not right?
With one argument, groupby assembles groups of equal consecutive elements: >>> list((key, list(group)) for key, group in groupby("AAABBCAAA")) [('A', ['A', 'A', 'A']), ('B', ['B', 'B']), ('C', ['C']), ('A', ['A', 'A', 'A'])] With a second keyfunc argument, groupby assembles groups where keyfunc(element) is equal for consecutive elements >>> list((key, list(group)) for key, group in groupby("AAAaaaAAA",str.isupper)) [(True, ['A', 'A', 'A']), (False, ['a', 'a', 'a']), (True, ['A', 'A', 'A'])] >>> HTH Michael -- http://mail.python.org/mailman/listinfo/python-list