Python通配符生成元音

2024-03-28 19:46:41 发布

您现在位置:Python中文网/ 问答频道 /正文

各位

通过开放课程软件自学python,我在一项作业中遇到了困难。你知道吗

基本上,用户输入一个单词,元音用*代替。 例如:“d*g”表示dig、dog或dug。你知道吗

我编写的程序将*替换为object:vouels='aeiou'中的值,然后查看单词列表,看看是否有匹配项。示例:d*g->;*dag:不匹配,deg:不匹配,dig:匹配;结束搜索。你知道吗

for char_vow in VOWELS:
  wildcard_word=word.replace('*',char_vow)
  print(wildcard_word)
  if wildcard_word in word_list:
      word=wildcard_word
      break

当有一个“*”时,这个方法非常有效,但是我的程序不能处理两个或更多。例如,如果用户输入d**th表示死亡,代码将只检查 达斯,迪斯,迪斯,杜斯,杜斯,然后给一个假的。你知道吗

所以我想递归可能是答案,然后写道:

def wildcard_replacement(word):
    wildcard_word=""
    if word.find('*')==-1:
        return word
    else:
        for char_vow in VOWELS:
              wildcard_word=word.replace('*',char_vow,1)
              print(wildcard_word)
              if wildcard_word in word_list:
                  word=wildcard_word
                  break
              elif wildcard_word.find('*')!=-1:
                  return wildcard_replacement(wildcard_word)
        return wildcard_replacement(wildcard_word)

    print(wildcard_replacement(word))

这个程序搜索:daath,daeth,daith,daoth,dauth然后停止。事后看来是有道理的。不再有*。但是我希望第一个元音现在从a到e翻转过来,然后继续第二个通配符的替换循环。我被困住了。。。你知道吗

有什么建议吗?你知道吗


Tags: 用户in程序forreturnif单词word
3条回答

递归函数是个好主意

def expand_vowels(word):

如果word中没有通配符,则生成不变的单词并使用return语句来表示递归结束

    if '*' not in word: yield word ; return

如果我们在这里,word中至少有一个'*'

    for new_word in (word.replace('*', vw, 1) for v in 'aeiou'):
        for new2_word in expand_vowels(new_word): yield new2_word

通过一些简单的测试,很容易把所有这些放在一起

$ cat vow.py
def expand_vowels(word):
    if '*' not in word: yield word ; return
    for new_word in (word.replace('*', v, 1) for v in 'aeiou'):
        for new2_word in expand_vowels(new_word): yield new2_word

for w in ('a', 'a*', 'a**'):
    print(list(expand_vowels(w)))

good = ['aei', 'bwe']

for wild in ('a**', 'b**'):
    for word in expand_vowels(wild):
        if word in good:
            print(word, 'is in the good words')
            break
    else:
        print(wild, 'doesn\'t generate a good word')
$ python vow.py
['a']
['aa', 'ae', 'ai', 'ao', 'au']
['aaa', 'aae', 'aai', 'aao', 'aau', 'aea', 'aee', 'aei', 'aeo', 'aeu', 'aia', 'aie', 'aii', 'aio', 'aiu', 'aoa', 'aoe', 'aoi', 'aoo', 'aou', 'aua', 'aue', 'aui', 'auo', 'auu']
aei is in the good words
b** doesn't generate a good word
$

你可以在这里做的是,使用itertools产品,你可以指定在death的例子中所需要的元音组合,它应该是2,然后我们可以设置repeat = 2,得到所有的2个字母的元音组合,然后我们可以用str.replace替换'**',直到我们得到一个匹配的元音组合wordbank

from itertools import product

vowels = 'aeiou'
wordbank = ['death']
word = 'd**th'
x = word.count('*')

l = [''.join(i) for i in [*product(vowels, repeat = x)]]
print(l)

for i in l:
    guess = word.replace('**', i)
    if guess in wordbank:
        print(guess)
        break
 death

这是因为您在elif分支中执行return(在for之下)。 所以基本上,你永远不会在你的上层重复所有的元音,因为一旦你停止了一个*并替换了它,就没有更多的了。。。你知道吗

建议(因为,看起来您实际上不需要“代码”回复):

  1. 检查单词是否属于你的字典(word_list)的限制大小写(即当没有*
  2. 总是向函数发送临时结果(在替换了*中的一个之后)
  3. 如果您想在第一个遇到有效单词时break,我将检查调用的wildcard_replacement的结果,而不是其他内容(实际上是对wildcard_word而不是返回值进行验证)

相关问题 更多 >