按给定模式创建新词的函数

2024-07-02 12:47:11 发布

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

我正在尝试创建一个函数,它接受一个单词(大写字母和小写字母)并将每个字符映射到一个新字符。模式是每个元音(AEIOU)依次成为下一个元音(A->;E,E->;I)。对于常量字母,则变为三分之一字母(B->;F,C->;G)

>>>'hello'
'lippu'
>>> 'today'
'xuhec'
>>> 'yesterday'
'ciwxivhec'

我知道我必须创建两个列表:

vowels = ['a', 'e', 'i', 'o', 'u']
constants = ['b', 'c','d','f','g','h','j','k','l','m','n','p', 'q','r', 's','t','v','w','x','y', 'z']

并使用index()函数,检查当前索引并向其中添加3,但之后我就卡住了。你知道吗

对于超出清单范围的案件,信件会循环出现。(x-z和u)


Tags: 函数gthellotoday字母模式大写字母字符
3条回答

我们可以使用itertools.cycle。首先检查哪个类别i属于vowelconsonants(不是常量)。然后从相应的列表中创建一个cycle,使用whilenext,直到找到相应的字母。如果是a vowel,我们简单地附加next值,如果是a consonant,我们前进2个位置,然后附加next值。使用.join()转换回字符串之后。你知道吗

from itertools import cycle

vwl = ['a', 'e', 'i', 'o', 'u']
cnst = ['b', 'c','d','f','g','h','j','k','l','m','n','p', 'q','r', 's','t','v','w','x','y', 'z']

s = 'hello'
new = []
for i in s.lower():
    if i in vwl:
        a = cycle(vwl)
        while i != next(a):
            next(a)    
        new.append(next(a))
    if i in cnst:
        b = cycle(cnst)
        while i != next(b):
            next(b)
        for x in range(2):
            next(b)
        new.append(next(b))

res = ''.join(new)
print(res)
# lippu

适用于包含边缘字母的单词,zumba产生daqfe

要计算映射,可以使用enumerate(获取当前的索引)和模(对于大于列表长度的索引),如下所示:

vowels = ['a', 'e', 'i', 'o', 'u']
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']

vowels_map = {k: vowels[(i + 1) % len(vowels)] for i, k in enumerate(vowels)}
consonants_map = {k: consonants[(i + 3) % len(consonants)] for i, k in enumerate(consonants)}

print(vowels_map)
print(consonants_map)

输出

{'u': 'a', 'a': 'e', 'o': 'u', 'e': 'i', 'i': 'o'}
{'s': 'w', 'z': 'd', 'v': 'y', 'm': 'q', 'f': 'j', 'h': 'l', 'd': 'h', 'g': 'k', 'q': 't', 'n': 'r', 'p': 's', 'k': 'n', 't': 'x', 'y': 'c', 'r': 'v', 'w': 'z', 'x': 'b', 'l': 'p', 'b': 'f', 'j': 'm', 'c': 'g'}

请注意,词典没有顺序,也就是说您可以按以下方式使用它们:

def replace_from_dict(word, table):
    return ''.join(table[c] for c in word)


words = ['hello',
         'today',
         'yesterday']

for word in words:
    print(replace_from_dict(word, { **vowels_map, **consonants_map }))

输出(从dict使用replace\u)

lippu
xuhec
ciwxivhec

我为edge case定义了两个字典,元音字典和一个字母x/y/z字典来实现包装。我遍历了字符串,如果这个字符是一个特殊的字符,我就使用适当的字典来查找这个单词。但是,如果字符在“w”以下,而不是元音,我只需在其ord值(ASCII值)中添加4,并将其转换为char。你知道吗

def transform(input):
  return_string = ""
  vowel_dictionary = {
    'a': 'e',
    'e': 'i',
    'i': 'o',
    'o': 'u',
    'u': 'a'
  }
  edge_dictionary = {
    'x': 'b',
    'y': 'c',
    'z': 'd'
  }
  for character in input.lower():
    if character in vowel_dictionary:
      return_string += vowel_dictionary[character]
    elif ord(character) <= ord("v"):
      return_string += chr(ord(character) + 4)
    else :
      return_string += edge_dictionary[character]

  return return_string

我已经用上面的代码运行了一些测试:

测试

transform("hello") # => lippu

transform("today") # => xuhec

transform("yesterday") # => ciwxivhec

相关问题 更多 >