[Tutor] anagrams

Daniel Ehrenberg littledanehren at yahoo.com
Wed Nov 5 17:34:17 EST 2003


> f = open("wordlist.txt")
> 
> d = {}
> for word in f:
>     k, v = list(word.lower()), word
>     k.sort()
>     k = ''.join(k)
>     if k in d:
>         d[k].append(v)
>     else:
>         d[k] = [v]
> 
> f.close()
> 
> ang = [x for x in d.itervalues() if len(x) > 1]
> 
> print len(ang)
> print sum([len(x) for x in ang])

For some reason, when I iterated over a file, it gave
me lines, not words. Is this what it is supposed to
do, or could there be something wrong with how I have
Python configured?

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree



More information about the Tutor mailing list