【发布时间】:2013-12-12 17:13:46
【问题描述】:
一段时间以来,我一直在使用此代码时遇到问题。代码应该做的是获取输入的单词,从输入的单词中删除字母,然后制作单词的子集并将其保存到数组中以进行打印。我的问题是这段代码将保存可以从输入的单词创建的每个单词。 Ex input= spot output= top tops stop pots pot, ect.
如果它只打印 tops、pots 和 stop 就好了。我做了一些调试打印,试图弄清楚程序实际上在做什么,但无济于事。我将留下我已注释掉的代码,因为这可能有助于了解我当时的心态/目标。
我已经搜索了谷歌和这个网站,但它并没有我正在寻找的答案。抱歉打扰了。
mylist = open('sortedwords.txt')
txt = mylist.read()
mylist = txt.split()
stuff = input('Type a word here: ')
def removeletters (word, Analysis):
for char in range (len(Analysis)):
if Analysis [char] in word:
word = word.replace(Analysis[char],"",1)
return word
def anagramSubset(word, textList):
newWord = word
for char in range(len(textList)):
if textList[char] not in newWord:
return False
else:
newWord = newWord.replace(textList[char],"",1)
return True
def anagram(word, textList):
savedWords =[]
for checkword in textList:
if len(word) == len(checkword) and anagramSubset(word, checkword):
savedWords.append(checkword)
print(checkword)
anagram(stuff, mylist)
我知道你们不是来为我工作的,但我真的很努力,但无法亲自找到导师。如果您能告诉我出了什么问题以及如何解决,我将不胜感激。
【问题讨论】:
-
sortedwords.txt和textlist在anagram方法中的用途是什么? “从输入的单词中删除一个字母”是什么意思?