[Tutor] How to list/process files with identical character strings
mark murphy
msmurphy at alumni.unc.edu
Tue Jun 24 23:01:03 CEST 2014
Hi Danny, Marc, Peter and Alex,
Thanks for the responses! Very much appreciated.
I will take these pointers and see what I can pull together.
Thanks again to all of you for taking the time to help!
Cheers,
Mark
On Tue, Jun 24, 2014 at 4:39 PM, Danny Yoo <dyoo at hashcollision.org> wrote:
> 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]
> #########################################
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
--
Mark S. Murphy
Alumnus
Department of Geography
msmurphy at alumni.unc.edu
951-252-4325
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140624/397963bd/attachment.html>
More information about the Tutor
mailing list