473,796 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

anagram finder / dict mapping question

Hi All,

I wrote a program that takes a string sequence and finds all the words
inside a text file (one word per line) and prints them:

def anagfind(letter s): #find anagrams of these letters
fin = open('text.txt' ) #one word per line file
wordbox = [] #this is where the words will go
for line in fin:
word = line.strip()
count = 0
for char in letters:
if char not in word:
break
else:
count += 1
if count == len(word):
wordbox.append( word)
return wordbox

Now I'd like to modify the code to naturally find all anagrams inside a
wordlist. What would be the best way to do this? Using Hints? Is it
possible to iterate over dict keys? How can I make a dict that maps
from a set of letters to a list of words that are spelled from those
letters? Wouldn't I need to make the set of letters a key in a dict?

As always - Thanks for helping someone trying to learn...

Dave

Jun 27 '08 #1
19 2806
On Thu, 08 May 2008 11:02:12 +1000, dave <sq************ *@1ya2hoo3.net
wrote:
Hi All,

I wrote a program that takes a string sequence and finds all the words
inside a text file (one word per line) and prints them:

def anagfind(letter s): #find anagrams of these letters
fin = open('text.txt' ) #one word per line file
wordbox = [] #this is where the words will go
for line in fin:
word = line.strip()
count = 0
for char in letters:
if char not in word:
break
else:
count += 1
if count == len(word):
wordbox.append( word)
return wordbox

Now I'd like to modify the code to naturally find all anagrams inside a
wordlist. What would be the best way to do this? Using Hints? Is it
possible to iterate over dict keys? How can I make a dict that maps
from a set of letters to a list of words that are spelled from those
letters? Wouldn't I need to make the set of letters a key in a dict?

As always - Thanks for helping someone trying to learn...

Dave
Suggestion: for each word, sort their characters and use them as the
dictionary key. If two words have the same combination of characters,
then they are anagrams. For example: "edam" and "made" are anagrams
because they have the letters 'a', 'd', 'e' and 'm'.

Refer "Programmin g Pearls" by Jon Bentley.

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
Jun 27 '08 #2
This is what i've came up with. My problem is that I can't get them to
properly evaluate.. when comparewords() runs it finds itself...
Should I have the keys of mapdict iterate over itself? Is that
possible?

def annafind():
fin = open('text.txt' ) # file has one word per line
mapdic = {} # each word gets sorted & goes in here
for line in fin:
rawword = line.strip()
word = list(rawword)
word.sort()
mapdic[''.join(word)] = 0
return mapdic
def comparewords(): ***not working as intended
fin = open('text.txt' )
for line in fin:
line = line.strip()
word = list(line)
word.sort()
sortedword = (''.join(word))
if sortedword in mapdic:
print line


On 2008-05-07 19:25:53 -0600, "Kam-Hung Soh" <ka*********@gm ail.comsaid:
On Thu, 08 May 2008 11:02:12 +1000, dave <sq************ *@1ya2hoo3.net>
wrote:
>Hi All,

I wrote a program that takes a string sequence and finds all the words
>inside a text file (one word per line) and prints them:

def anagfind(letter s): #find anagrams of these letters
fin = open('text.txt' ) #one word per line file
wordbox = [] #this is where the words will go
for line in fin:
word = line.strip()
count = 0
for char in letters:
if char not in word:
break
else:
count += 1
if count == len(word):
wordbox.append( word)
return wordbox

Now I'd like to modify the code to naturally find all anagrams inside
a
>wordlist. What would be the best way to do this? Using Hints? Is it
>possible to iterate over dict keys? How can I make a dict that maps
>from a set of letters to a list of words that are spelled from those
>letters? Wouldn't I need to make the set of letters a key in a dict?

As always - Thanks for helping someone trying to learn...

Dave

Suggestion: for each word, sort their characters and use them as the
dictionary key. If two words have the same combination of characters,
then they are anagrams. For example: "edam" and "made" are anagrams
because they have the letters 'a', 'd', 'e' and 'm'.

Refer "Programmin g Pearls" by Jon Bentley.

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</ a>

Jun 27 '08 #3
On Thu, 08 May 2008 15:42:07 +1000, dave <sq************ *@1ya2hoo3.net
wrote:
This is what i've came up with. My problem is that I can't get them to
properly evaluate.. when comparewords() runs it finds itself... Should
I have the keys of mapdict iterate over itself? Is that possible?

def annafind():
fin = open('text.txt' ) # file has one word per line
mapdic = {} # each word gets sorted & goes in here
for line in fin:
rawword = line.strip()
word = list(rawword)
word.sort()
mapdic[''.join(word)] = 0
return mapdic
def comparewords(): ***not working as intended
fin = open('text.txt' )
for line in fin:
line = line.strip()
word = list(line)
word.sort()
sortedword = (''.join(word))
if sortedword in mapdic:
print line


On 2008-05-07 19:25:53 -0600, "Kam-Hung Soh" <ka*********@gm ail.com
said:
>On Thu, 08 May 2008 11:02:12 +1000, dave <sq************ *@1ya2hoo3.net>
wrote:
>>Hi All,
I wrote a program that takes a string sequence and finds all the words
>>inside a text file (one word per line) and prints them:
def anagfind(letter s): #find anagrams of these letters
fin = open('text.txt' ) #one word per line file
wordbox = [] #this is where the words will go
for line in fin:
word = line.strip()
count = 0
for char in letters:
if char not in word:
break
else:
count += 1
if count == len(word):
wordbox.append( word)
return wordbox
Now I'd like to modify the code to naturally find all anagrams inside
a
>>wordlist. What would be the best way to do this? Using Hints? Is it
>>possible to iterate over dict keys? How can I make a dict that maps
>>from a set of letters to a list of words that are spelled from those
>>letters? Wouldn't I need to make the set of letters a key in a dict?
As always - Thanks for helping someone trying to learn...
Dave
Suggestion: for each word, sort their characters and use them as the
dictionary key. If two words have the same combination of characters,
then they are anagrams. For example: "edam" and "made" are anagrams
because they have the letters 'a', 'd', 'e' and 'm'.
Refer "Programmin g Pearls" by Jon Bentley.
--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</
a>

Your code is always going to return the same list because every word is an
anagram of itself.

Tip: Create a list for each dictionary key, then add a word to the list if
that word is not in the list. So:

mapdic('adem') --["edam", "made"]

P.S. When replying, the convention is to add your response to the bottom,
not top of the message.

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
Jun 27 '08 #4
>On 2008-05-07 19:25:53 -0600, "Kam-Hung Soh" <ka*********@gm ail.com>
>
>said:
>>On Thu, 08 May 2008 11:02:12 +1000, dave <sq************ *@1ya2hoo3.ne
t>
>>wrote:

Hi All,
I wrote a program that takes a string sequence and finds all the wo
rds
>>>
inside a text file (one word per line) and prints them:
def anagfind(letter s): #find anagrams of these letters
fin = open('text.txt' ) #one word per line file
wordbox = [] #this is where the words will go
for line in fin:
word = line.strip()
count = 0
for char in letters:
if char not in word:
break
else:
count += 1
if count == len(word):
wordbox.append( word)
return wordbox
Now I'd like to modify the code to naturally find all anagrams insi
de
>>a
wordlist. What would be the best way to do this? Using Hints? Is
it
>>>
possible to iterate over dict keys? How can I make a dict that maps
>>>
from a set of letters to a list of words that are spelled from those
>>>
letters? Wouldn't I need to make the set of letters a key in a dict
?
>>> As always - Thanks for helping someone trying to learn...
Dave

Suggestion: for each word, sort their characters and use them as the
>>dictionary key. If two words have the same combination of characters
,
>>then they are anagrams. For example: "edam" and "made" are anagrams
because they have the letters 'a', 'd', 'e' and 'm'.
Refer "Programmin g Pearls" by Jon Bentley.
--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salarima
n</
>>a>


Your code is always going to return the same list because every word is an
anagram of itself.

Tip: Create a list for each dictionary key, then add a word to the list if
that word is not in the list. So:

mapdic('adem') --["edam", "made"]

P.S. When replying, the convention is to add your response to the bottom ,
not top of the message.

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</ a>
I got it! Thanks for all your help!!! Please tell me what you think:

def anafind():
fin = open('short.txt ') #one word per line
mapdic = {} #this dic maps letters to anagrams
for line in fin:
line = line.strip()
templist = sorted(list(lin e)) #sort letters
newtlist = (''.join(templi st)) #join letters
if newtlist not in mapdic:
mapdic[newtlist] = [line]
if line not in mapdic[newtlist]:
mapdic[newtlist].append(line)
for value in mapdic.values() :
if len(value) <= 1:
pass
else:
print value
Jun 27 '08 #6
dave <sq************ *@1ya2hoo3.netw rites:
if newtlist not in mapdic:
mapdic[newtlist] = [line]
if line not in mapdic[newtlist]:
mapdic[newtlist].append(line)
I'd use the defaultdict module to avoid the above.
for value in mapdic.values() :
if len(value) <= 1:
pass
else:
print value
I'd write:

for value in mapdic.values() :
if len(value) 1:
print value

I'd actually use itervalues rather than values, but that's a little
bit fancy, you don't have to worry about it til later.
Jun 27 '08 #7
On Fri, 09 May 2008 09:52:53 +1000, dave <sq************ *@1ya2hoo3.net
wrote:
I got it! Thanks for all your help!!! Please tell me what you think:

def anafind():
fin = open('short.txt ') #one word per line
mapdic = {} #this dic maps letters to anagrams
for line in fin:
line = line.strip()
templist = sorted(list(lin e)) #sort letters
newtlist = (''.join(templi st)) #join letters
if newtlist not in mapdic:
mapdic[newtlist] = [line]
if line not in mapdic[newtlist]:
mapdic[newtlist].append(line)
for value in mapdic.values() :
if len(value) <= 1:
pass
else:
print value

I avoid creating a temporary variable if it's only used once immediately,
so I would have written:

newtlist = ''.join(sorted( list(line)))

Looks pretty good, and I found the other tips in parallel responses useful
to me too!

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
Jun 27 '08 #8
On Thu, May 8, 2008 at 7:52 PM, dave <sq************ *@1ya2hoo3.netw rote:
I got it! Thanks for all your help!!! Please tell me what you think:
Here's yet another version of the same thing, using defaultdicts and
sets. This way we don't have to test for membership in either the
dictionary or the collection of anagrams. The code is shorter, but no
less readable in my opinion.

from collections import defaultdict

def anafind(words):
"""
Given a sequence of words, return a dictionary with groups
of letters as keys and sets of anagrams as values
"""
anagrams = defaultdict(set )
for word in words:
key = ''.join(sorted( word))
anagrams[key].add(word)
return anagrams

if __name__ == "__main__":
wordlist = ['live', 'evil', 'one', 'nose', 'vile', 'neo']
anagrams = anafind(wordlis t)
for letters, words in anagrams.items( ):
print "%s: %s" % (letters, words)

--
Jerry
Jun 27 '08 #9
"Kam-Hung Soh" <ka*********@gm ail.comwrites:
I avoid creating a temporary variable if it's only used once
immediately, so I would have written:

newtlist = ''.join(sorted( list(line)))
Or ''.join(sorted( line))

as sorted() works with any iterable.

--
Arnaud
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2156
by: GrelEns | last post by:
hello, i would like if this behaviour can be obtained from python : trap an attributeError from inside a subclassing dict class... (here is a silly examples to explain my question) class Test(dict): """subclassing a dict and each key will permit access to a tuple of fized size""" def __init__(self):
17
4295
by: Pierre Fortin | last post by:
Hi, Is the following a reasonable generic approach for converting python returned tuples/lists into dicts...? I'm not advocating library functions also return dicts (I'd probably spend more time looking up the real names... :) I'm just looking to make my code more readable and self-documenting... --------
25
2394
by: Steven Bethard | last post by:
So I end up writing code like this a fair bit: map = {} for key, value in sequence: map.setdefault(key, ).append(value) This code basically constructs a one-to-many mapping -- each value that a key occurs with is stored in the list for that key. This code's fine, and seems pretty simple, but thanks to generator
7
2335
by: Lowell Kirsh | last post by:
I have a script which I use to find all duplicates of files within a given directory and all its subdirectories. It seems like it's longer than it needs to be but I can't figure out how to shorten it. Perhaps there are some python features or libraries I'm not taking advantage of. The way it works is that it puts references to all the files in a dictionary with file size being the key. The dictionary can hold multiple values per key....
8
1435
by: OPQ | last post by:
Hi all, I'd happy to have you share some thougts about ultimate optimisations on those 2 topics: (1)- adding one caractere at the end of a string (may be long) (2)- in a dict mapping a key to a list of int, remove every entrie where the list of int have of length < 2
7
3101
by: Marcio Rosa da Silva | last post by:
Hi! In dictionaries, unlinke lists, it doesn't matter the order one inserts the contents, elements are stored using its own rules. Ex: >>> d = {3: 4, 1: 2} >>> d {1: 2, 3: 4}
2
1980
by: Alex | last post by:
Entering >>> help(dict) Help on class dict in module __builtin__: class dict(object) | dict() -> new empty dictionary. | dict(mapping) -> new dictionary initialized from a mapping object's | (key, value) pairs. | dict(seq) -> new dictionary initialized as if via:
0
353
by: barnesc | last post by:
Nifty, Tim Peters responded to my e-mail. I must've said something interesting. Cool, a PyCelebrity! > >> ... >> I've gotten bored and went back to one of my other projects: >> reimplementing the Python builtin classes list(), set(), dict(), >> and frozenset() with balanced trees (specifically, counted B-trees >> stored in memory). >>
3
1750
by: bruce | last post by:
hi guys/gals... got a basic question that i can't get my hands around. i'm trying to programatically create/use a list/tuple (or whatever the right phrase in pyton is!!) basically, something like: foo = foo.append('cat')
0
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.