【问题标题】:Pig Latin- keep non-letters pythonPig Latin - 保持非字母 python
【发布时间】:2018-08-19 08:57:12
【问题描述】:

我目前正在编写一个猪拉丁语代码,它要求忽略所有非字母,即留在单词的同一位置。例如,Tom's 将是 om'sTay。所以我使用以下方法删除了所有非字母字符: word = re.sub('[^A-Za-z]', '', word) 做了猪拉丁代码,我想把非字母带回来。 不知道如何将非字母带回更改单词的相同位置。

【问题讨论】:

  • ...不要删除它们?保留备份?你的具体问题是什么?
  • 听起来很有趣。你有问题吗?
  • 我还是不明白你在问什么。你能澄清一下吗?上面链接的问题对您有帮助吗?
  • @DeliriousLettuce 是的 输入:苹果!ple 是绿色 输出:ethay ap!plehay ishay eengray 或输入:Kate's care 输出:ate'sKay arecay 谢谢

标签: python regex non-alphanumeric


【解决方案1】:
In[2]: def pig_latinize(word):
  ...:     cluster_dex = 0
  ...:     vowels = set('AEIOUaeiou')
  ...:     for i, char in enumerate(word, start=1):
  ...:         if not char.isalpha() or char in vowels:
  ...:             break
  ...:         cluster_dex = i
  ...:     if cluster_dex == 0:
  ...:         suffix = 'hay'
  ...:     elif cluster_dex == len(word):
  ...:         cluster_dex = 1
  ...:         suffix = 'way'
  ...:     else:
  ...:         suffix = 'ay'
  ...:     return '{}{}{}'.format(word[cluster_dex:], word[:cluster_dex], suffix)
  ...: 
  ...: 
  ...: def pig_latin(words):
  ...:     return ' '.join(pig_latinize(word) for word in words.split())
  ...: 
In[3]: pig_latin("Tom's") == "om'sTay"
Out[3]: True
In[4]: pig_latin('the ap!ple is green') == 'ethay ap!plehay ishay eengray'
Out[4]: True
In[5]: pig_latin("Kate's care") == "ate'sKay arecay"
Out[5]: True
In[6]: pig_latin('myth') == 'ythmway'
Out[6]: True
In[7]: pig_latin('cry') == 'rycway'
Out[7]: True

编辑:

这显示了将这些函数从您提供的描述转换为类的可能方法

>>> class PigLatin(object):
...     VOWELS = set('AEIOUaeiou')
...
...     def __init__(self, to_convert):
...         self.convert_words(to_convert)
...
...     def convert_words(self, to_convert):
...         if not isinstance(to_convert, list):
...             # if to_convert is a string, split into separate words
...             to_convert = to_convert.split()
...         self.pig_latin = ' '.join(
...             self.p_latin_converter(word) for word in to_convert)
...
...     def p_latin_converter(self, word):
...         cluster_dex = 0
...         for i, char in enumerate(word, start=1):
...             if not char.isalpha() or char in self.VOWELS:
...                 break
...             cluster_dex = i
...         if cluster_dex == 0:
...             suffix = 'hay'
...         elif cluster_dex == len(word):
...             cluster_dex = 1
...             suffix = 'way'
...         else:
...             suffix = 'ay'
...         return '{}{}{}'.format(word[cluster_dex:], word[:cluster_dex], suffix)
...

# *** An object of this method initializes the string it wants to 
# convert when it is created. ***
>>> p = PigLatin("Tom's")  # initialize with a sting
# pig_latin attribute is then set with the converted text
>>> p.pig_latin == "om'sTay"
True

# *** Further, the class should have a method that allows the string to be
# modified to perform another conversion. ***
>>> p.convert_words('the ap!ple is green')  # modify the pig_latin attribute
>>> p.pig_latin == 'ethay ap!plehay ishay eengray'
True

# *** build a pigLatin_class that works with strings and lists and has a
# method to convert a string into pig latin. ***
>>> p.convert_words(['myth', 'cry'])  # use a list of strings
>>> p.pig_latin == 'ythmway rycway'
True

# *** The set of VOWELS can be defined as a static attribute of the class. ***
>>> p.VOWELS
{'E', 'e', 'A', 'O', 'a', 'i', 'u', 'I', 'U', 'o'}

# *** The class pigLatin should be based on the basic object datatype. ***
>>> isinstance(p, object)  # inherits from object "class PigLatin(object):"
True

# *** method pLatin_converter can be used. ***
>>> p.pig_latin  # current pig_latin attribute
'ythmway rycway'
# returns a word without modifying the pig_latin attribute
>>> p.p_latin_converter("Kate's care")
"ate's careKay"
# show that the pig_latin attribute wasn't modified after the above call
>>> p.pig_latin
'ythmway rycway'

【讨论】:

  • 非常感谢!但是输入:cry 应该输出:rycway
  • yes 对于不带元音的单词,最后一个字母到末尾,后缀方式加输入:神话输出:ythmway
  • 你能解释一下“{}{}{}”是什么意思吗
  • 谢谢!我必须将 pig latin 更改为一个类,并且由于有两个定义,我很难理解要添加哪些变量 self.到。
  • 谢谢!我们必须构建一个将字符串转换为拉丁语的类,它应该基于基本的对象数据类型。该类应该有一个允许修改字符串以执行另一个转换的方法。
猜你喜欢
  • 1970-01-01
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多