anagram finder / dict mapping问题 [英] anagram finder / dict mapping question

查看:66
本文介绍了anagram finder / dict mapping问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我写了一个程序,它接受一个字符串序列并在文本文件中找到所有单词

(每行一个单词)并打印出来:


def anagfind(字母):#find这些字母的字谜

fin = open(''text.txt'')#one每行文字

wordbox = []#这是单词行的地方

for fin:

word = line.strip ()

count = 0

for char in letters:

如果char不在单词中:

break

else:

count + = 1

if count == len(word):

wordbox.append(单词)

返回wordbox


现在我想修改代码,自然地找到

单词表中的所有字谜。最好的方法是什么?使用提示?是否可以迭代dict键来获得
?我怎样才能制作一个字母,从一组字母映射到一个字母列表,从那些

字母拼写出来?难道我不需要把这套字母作为字典中的一个关键吗?


一如既往 - 感谢帮助有人试图学习...

Dave

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(letters): #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

推荐答案

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:

大家好,


我编写了一个程序,它接受一个字符串序列并在文本文件中找到所有单词

(每行一个单词)并打印出来:

def anagfind(字母):#find这些字母的字谜

fin = open(''text.txt'')每行文件#one word

wordbox = []#这就是单词将去的地方

for fin:

word = line.strip()

count = 0

for char in letters:

如果char不在单词中:

break

else:

count + = 1

if count == len(word):

wordbox.append(单词)

返回wordbox


现在我想修改代码以自然地找到

wordlist。最好的方法是什么?使用提示?是否可以迭代dict键来获得
?我怎样才能制作一个字母,从一组字母映射到一个字母列表,从那些

字母拼写出来?难道我不需要把这套字母作为字典中的一个关键吗?


一如既往 - 感谢帮助有人试图学习...

Dave
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(letters): #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



建议:对于每个单词,对其字符进行排序并将其用作

字典键。如果两个单词具有相同的字符组合,那么它们就是字谜。例如: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 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 "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 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*********@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 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

a


> 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 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 "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.net
wrote:

这就是我想出来的。我的问题是,我无法让他们

正确评估..当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 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*********@gmail.com
said:

> 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 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

a


>> 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 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 "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 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>


这篇关于anagram finder / dict mapping问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆