[Tutor] How to list/process files with identical character strings

Danny Yoo dyoo at hashcollision.org
Tue Jun 24 22:39:04 CEST 2014


The sorting approach sounds reasonable.  We might even couple it with
itertools.groupby() to get the consecutive grouping done for us.

    https://docs.python.org/2/library/itertools.html#itertools.groupby


For example, the following demonstrates that there's a lot that the
library will do for us that should apply directly to Mark's problem:

#########################################
import itertools
import random

def firstTwoLetters(s): return s[:2]

grouped = itertools.groupby(
    sorted(open('/usr/share/dict/words')),
    key=firstTwoLetters)

for k, g in grouped:
    print k, list(g)[:5]
#########################################


More information about the Tutor mailing list