DaveHi All,I wrote a program that takes a string sequence and finds all the wordsinside a text file (one word per line) and prints them:def anagfind(letters): #find anagrams of these lettersfin = open(''text.txt'')#one word per line filewordbox = [] #this is where the words will gofor line in fin:word = line.strip()count = 0for char in letters:if char not in word:breakelse:count += 1if count == len(word):wordbox.append(word)return wordboxNow I''d like to modify the code to naturally find all anagrams inside awordlist. What would be the best way to do this? Using Hints? Is itpossible to iterate over dict keys? How can I make a dict that mapsfrom a set of letters to a list of words that are spelled from thoseletters? 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 建议:对于每个单词,对其字符进行排序并将其用作 字典键。如果两个单词具有相同的字符组合,那么它们就是字谜。例如:edam和制造是字谜 ,因为他们有'a','d'','e''和'm'字母。 请参阅编程珍珠。作者:Jon Bentley。 - Kam-Hung Soh< a href =" http://kamhungsoh.com/blog">软件Salariman< / a>Suggestion: for each word, sort their characters and use them as thedictionary key. If two words have the same combination of characters,then they are anagrams. For example: "edam" and "made" are anagramsbecause they have the letters ''a'', ''d'', ''e'' and ''m''.Refer "Programming Pearls" by Jon Bentley.--Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>这就是我想出来的。我的问题是,我无法让他们正确评估..当比较词()运行它发现自己... 我应该有mapdict的键吗?迭代自己?那是 可能吗? def annafind(): fin = open(''text.txt'')#文件每行有一个单词 mapdic = {}#每个单词都被排序&进入这里 for line in fin: rawword = line.strip() word = list(rawword) word.sort() mapdic [''''。join(word)] = 0 返回mapdic def comparewords ():***没有按预期工作 fin = open(''text.txt'') for line in fin: line = line.strip() word = list(line) word.sort() sortedword =('''' .join(word)) 如果在mapdic中排序: 打印行 2008-05- 07 19:25:53 -0600,Kam-Hung Soh < ka ********* @ gmail.comsaid:This is what i''ve came up with. My problem is that I can''t get them toproperly evaluate.. when comparewords() runs it finds itself...Should I have the keys of mapdict iterate over itself? Is thatpossible?def annafind():fin = open(''text.txt'')# file has one word per linemapdic = {}# each word gets sorted & goes in herefor line in fin:rawword = line.strip()word = list(rawword)word.sort()mapdic[''''.join(word)] = 0return mapdicdef comparewords(): ***not working as intendedfin = open(''text.txt'')for line in fin:line = line.strip()word = list(line)word.sort()sortedword = (''''.join(word))if sortedword in mapdic:print lineOn 2008-05-07 19:25:53 -0600, "Kam-Hung Soh" <ka*********@gmail.comsaid: On Thu,2008年5月8日11:02:12 +1000,dave< sq*************@@ya2hoo3.net> 写道: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 >每行一字)并打印出来: def anagfind(字母):#find这些字母的字谜 fin = open(''text.txt'')#one word per line file wordbox = []#这是单词将用于行中的单词: word = line.strip() count = 0 for char in信件:如果char不在字中: break 否则: count + = 1 如果count == len(字): wordbox。追加(word)返回wordbox 现在我想修改代码,自然找到里面的所有字谜>inside a text file (one word per line) and prints them:def anagfind(letters): #find anagrams of these lettersfin = open(''text.txt'')#one word per line filewordbox = [] #this is where the words will gofor line in fin:word = line.strip()count = 0for char in letters:if char not in word:breakelse:count += 1if count == len(word):wordbox.append(word)return wordboxNow I''d like to modify the code to naturally find all anagrams inside aa > wordlist。最好的方法是什么?使用提示?是>wordlist. What would be the best way to do this? Using Hints? Is it >可以迭代dict键吗?如何制作一个dict,将>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 建议:对于每个单词,对其字符进行排序并将其用作 字典键。如果两个单词具有相同的字符组合,那么它们就是字谜。例如:edam和制造是字谜 ,因为他们有'a','d'','e''和'm'字母。 请参阅编程珍珠。作者:Jon Bentley。 - Kam-Hung Soh< a href =" http://kamhungsoh.com/blog">软件萨拉里曼< / a>Suggestion: for each word, sort their characters and use them as thedictionary key. If two words have the same combination of characters,then they are anagrams. For example: "edam" and "made" are anagramsbecause they have the letters ''a'', ''d'', ''e'' and ''m''.Refer "Programming Pearls" by Jon Bentley.--Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</ a> 2008年5月8日星期四15:42:07 +1000,dave< sq ********* ****@1ya2hoo3.net 写道:On Thu, 08 May 2008 15:42:07 +1000, dave <sq*************@1ya2hoo3.netwrote: 这就是我想出来的。我的问题是,我无法让他们 正确评估..当comparewords()运行它发现自己...应该 我有mapdict的键迭代自己?这可能吗? def annafind(): fin = open(''text.txt'')#file每行一个字 mapdic = {}#每个单词都被排序&进入这里 for line in fin: rawword = line.strip() word = list(rawword) word.sort() mapdic [''''。join(word)] = 0 返回mapdic def comparewords():***无法正常工作 fin = open(''text.txt'') for line in fin: line = line.strip() word = list(line) word.sort() sortedword =( ''''。join(word)) 如果在mapdic中排序: 打印行 开2008-05-07 19:25:53 -0600,Kam-Hung Soh < ka ********* @ gmail.com 说:This is what i''ve came up with. My problem is that I can''t get them toproperly evaluate.. when comparewords() runs it finds itself... ShouldI have the keys of mapdict iterate over itself? Is that possible?def annafind():fin = open(''text.txt'')# file has one word per linemapdic = {}# each word gets sorted & goes in herefor line in fin:rawword = line.strip()word = list(rawword)word.sort()mapdic[''''.join(word)] = 0return mapdicdef comparewords(): ***not working as intendedfin = open(''text.txt'')for line in fin:line = line.strip()word = list(line)word.sort()sortedword = (''''.join(word))if sortedword in mapdic:print lineOn 2008-05-07 19:25:53 -0600, "Kam-Hung Soh" <ka*********@gmail.comsaid: > 2008年5月8日星期四11:02:12 +1000,dave< sq ************* @ 1ya2hoo3.net> 写道:>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 >> ;在文本文件中(每行一个单词)并打印出来: def anagfind(字母):#find这些字母的字谜 fin = open(''text.txt'')#one word每行文件 wordbox = []#这是单词行的地方 for fin: word = line.strip() count = 0 对于字母中的字符:如果字母不在字中:中断否则:计数+ = 1 如果count == len(字): wordbox.append(word)返回wordbox 现在我想修改代码,自然找到里面的所有字谜>>inside a text file (one word per line) and prints them: def anagfind(letters): #find anagrams of these lettersfin = open(''text.txt'')#one word per line filewordbox = [] #this is where the words will gofor line in fin:word = line.strip()count = 0for char in letters:if char not in word:breakelse:count += 1if count == len(word):wordbox.append(word)return wordbox Now I''d like to modify the code to naturally find all anagrams inside aa >> wordlist。最好的方法是什么?使用提示?是>>wordlist. What would be the best way to do this? Using Hints? Is it >>可以迭代dict键吗?如何制作一个dict,将>>possible to iterate over dict keys? How can I make a dict that maps >>从一组字母映射到拼写为$ b $的单词列表b>>from a set of letters to a list of words that are spelled from those >>字母?难道我不需要让这套字母成为字典中的关键吗?一如既往 - 感谢帮助有人试图学习...... Dave>>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 建议:对于每个单词,对其字符进行排序并将其用作字典键。如果两个单词具有相同的字符组合,那么它们就是字谜。例如:edam和制造是字谜因为他们有字母a,d,e和m。请参阅编程珍珠。作者:Jon Bentley。 - Kam-Hung Soh< a href =" http://kamhungsoh.com/blog">软件Salariman< / a> Suggestion: for each word, sort their characters and use them as thedictionary key. If two words have the same combination of characters,then they are anagrams. For example: "edam" and "made" are anagramsbecause they have the letters ''a'', ''d'', ''e'' and ''m''. Refer "Programming Pearls" by Jon Bentley. --Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</ a> 你的代码总是会返回相同的列表,因为每个单词都是一个 anagram本身。 提示:为每个字典键创建一个列表,然后在列表中没有该字的情况下向列表中添加一个单词 。所以: mapdic(''adem'') - [" edam"," made"] P.S.回复时,约定是将您的回复添加到底部, 不是消息的顶部。 - Kam-Hung Soh< a href =" http://kamhungsoh.com/blog">软件Salariman< / a>Your code is always going to return the same list because every word is ananagram of itself.Tip: Create a list for each dictionary key, then add a word to the list ifthat 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> 这篇关于anagram finder / dict mapping问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-11 06:24