Blog

Find words with the most anagrams efficiently using python

Following my previous post about 9 letter anagrams I am posting the final code I have created taking into account suggestions/snippets from Michael, Toby and Martin. Added two variables to make it nice and easy to modify what to look for.

Code

# -*- coding: utf-8 -*-
from time import time
from collections import defaultdict  

ag_len = 10 # Anagram word length
ag_min = 2  # Min # of anagrams
dictionary_path = '/usr/share/dict/british-english'
tic = time()

wd = defaultdict(set)  
for l in open (dictionary_path, 'r'):
	l=l.strip()
	if ag_len==len(l):
		wd["".join(sorted(l))].add (l)
	
for ws, wl in wd.iteritems():
	if len ( wl ) >= ag_min:
		print " ".join ( wl )


toc = time()
print toc-tic,'s' 

Explanation
The dictionary file is filtered by length into a dictionary. The key for the dictionary is the letter of the word sorted in order, IE:

"".join(sorted('arranging')) = 'aagginnrr'

With the value as the unsorted word. Because words that are an anagram of each other will be identical when sorted this means that using the add method with a dictionary will cause any anagram to share the same key. Eg:

When the dictionary gets to megatons it will create a new key in the dicitonary like so:
{'aegmnost': set(['megatons'])}

Then to magnetos
{'aegmnost': set(['magnetos', 'megatons'])}

Then to montages:
{'aegmnost': set(['magnetos', 'megatons', 'montages'])}

Then we loop over all the items in the dictionary we created and see if the length of the values is greater than the minimum value we are looking for.

All done, a very elegant and simple method to find words with several anagrams for a given word length.

Results

I was going to post the interesting 10 letter anagrams I found however I couldn’t find any with more than 2 anagrams with the dictionary I was using.

There is a 11 letter tripple anagram:

anthologies anthologise theologians

and some 8 letter with 4 or more anagrams:

painters pertains pantries repaints
resident nerdiest inserted trendies
salesmen lameness nameless maleness
strainer restrain terrains retrains trainers
altering triangle relating integral alerting
rangiest ingrates angriest gantries
parroted predator teardrop prorated
iterates teariest treatise treaties
trounces counters recounts construe

24 Comments

  1. best earbuds of 2015

    There is certainly a lot to learn about this subject. I like all the points you have made.

  2. Timothy

    Nice post. I learn something new and challenging on websites I stumbleupon on a daily basis.
    It’s always useful to read through content from other writers and practice something
    from their sites.

  3. migliori tablet

    At this time I am going to do my breakfast, later than having my breakfast coming yet again to
    read additional news.

  4. hote telescopice

    It’s very simple to find out any matter on web as compared to books,
    as I found this paragraph at this web page.

  5. May

    Hello there! This article couldn’t be written much better!
    Looking at this article reminds me of my previous roommate!
    He constantly kept talking about this. I most certainly will send this article to him.
    Fairly certain he’ll have a very good read.
    Many thanks for sharing!

  6. comparativa vigilabebes

    whoah this blog is magnificent i like reading your posts.
    Keep up the good work! You understand, lots of
    persons are searching round for this information,
    you can aid them greatly.

  7. Tony

    I luckily discovered your website whilst I was searching the web for a complete list of British words ranging from 4 – 12 letters that could be anagramised. The list must not contain proper nouns; the “clue” to the word can either be one word or a phrase. i.e. MENIAL COP, POLICEMAN.
    I have begun to compile a list myself containing around 2000 words, but it is a long laborious job doing it by trial and error.

  8. asse da stiro

    I read this post completely about the comparison of hottest and preceding technologies,
    it’s amazing article.

  9. destructeur de documents

    I am really loving the theme/design of your blog. Do
    you ever run into any browser compatibility issues?
    A handful of my blog audience have complained about my blog not working
    correctly in Explorer but looks great in Firefox. Do you have any tips
    to help fix this problem?

  10. ponceuses classement

    Superb, what a weblog it is! This blog provides helpful facts
    to us, keep it up.

  11. regolabarba

    Good site you’ve got here.. It’s difficult to find quality
    writing like yours these days. I truly appreciate individuals
    like you! Take care!!

  12. Jacklyn

    I’m amazed, I must say. Rarely do I encounter a blog that’s both
    educative and interesting, and let me tell you, you’ve
    hit the nail on the head. The issue is something that
    too few men and women are speaking intelligently about.
    I’m very happy I stumbled across this during my hunt
    for something relating to this.

  13. szybkowary ranking

    whoah this blog is fantastic i love reading your posts.
    Stay up the good work! You recognize, lots of people are looking round for this information, you could help them greatly.

  14. enricogambini.com

    I do not know if it’s just me or if perhaps
    everyone else experiencing issues with your blog. It appears like some of the
    written text on your posts are running off the screen. Can somebody else please comment and let me know if this is happening to them too?
    This may be a issue with my web browser because I’ve had this happen before.
    Kudos

  15. epilatoare

    Because the admin of this site is working, no hesitation very shortly it will be well-known, due
    to its quality contents.

  16. mejor cepillo electrico

    I create a comment whenever I appreciate a post
    on a site or I have something to contribute to the
    discussion. Usually it is triggered by the passion communicated in the article I looked at.
    And after this article Find words with the most
    anagrams efficiently using python | Stealthcopter.

    I was moved enough to post a thought 🙂 I actually do have
    2 questions for you if you usually do not mind. Is it simply me or do some of the remarks appear as if
    they are left by brain dead people? 😛 And, if you are posting on additional online social sites, I’d like to follow you.
    Would you make a list the complete urls of your communal pages like your Facebook page,
    twitter feed, or linkedin profile?

  17. comparatif tournedisques

    Usually I don’t learn post on blogs, but I would like to say that this write-up very forced me to check
    out and do so! Your writing taste has been amazed me.
    Thanks, very great post.

  18. tenda campeggio

    Good site you have here.. It’s difficult to find quality writing like yours nowadays.
    I seriously appreciate individuals like you!
    Take care!!

  19. hockeyuisp.com

    Remarkable! Its really awesome article, I have got much clear idea regarding from this article.

  20. Mejor Mini horno

    When some one searches for his required thing, therefore he/she wishes to
    be available that in detail, therefore that thing
    is maintained over here.

  21. bateria de cocina buena

    Right now it appears like Movable Type is the preferred blogging platform out there right now.

    (from what I’ve read) Is that what you are using on your blog?

  22. mg pharmacy rx levitra

    Florida for viagra sale mg pharmacy rx http://mgpharmacyrx.com

  23. cartier love bangle screwdriver fake

    cartierlovejesduas I kiss both my kids on the mouth, it’s how I grew up. I’m 30 and still kiss my mom on the lips every once in a great while. I don’t see anything wrong with it and never have. If you don’t like kissing your kids on the mouth then it’s your choice, but seriously?? Because of germs and cavities? Really? I mean if you have a cold sore then yes, common sense you don’t kiss them when you have a cold sore, you don’t kiss anyone with a cold sore, but my 4.5 year old will not go in the doors of his school unless he gets a kiss from me and it has to be on the mouth. Again, it’s your choice how and where you kiss your kids, I just never saw a problem with it, it’s how I was raised.
    cartier love bangle screwdriver fake http://www.banglegold.com/

  24. emergens.net

    May I simply just say what a relief to discover somebody who actually understands what they are
    talking about on the internet. You actually know how to bring a
    problem to light and make it important. More people have to look at this and understand this side of your story.
    I was surprised you aren’t more popular because you surely have the gift.

Leave a Comment

Your email address will not be published. Required fields are marked *