【发布时间】:2021-04-25 08:22:37
【问题描述】:
免责声明,抱歉,如果我没有明确表达我的问题。术语对我来说仍然是新事物。提前感谢您的阅读。
好的,我有一个名为
的函数def pluralize(word)
目的是使文件中的所有名词复数。我想要的输出是: {'plural': word_in_plural, 'status' : x} word_in_plural 是输入参数 (word) 的复数形式,而 x 是可以具有以下值之一的字符串; 'empty_string'、'proper_noun'、'already_in_plural'、'success'。
到目前为止,我的代码看起来像..
filepath = '/proper_noun.txt'
def pluralize(word):
proper_nouns = [line.strip() for line in open (filepath)] ### reads in file as list when function is called
dictionary = {'plural' : word_in_plural, 'status', : x} ### defined dictionary
if word == '': ### if word is an empty string, return values; 'word_in_plural = '' and x = 'empty_string'
dictionary['plural'] = ''
dictionary['status'] = 'empty_string'
return dictionary
您在上面看到的是我尝试编写一个条件,如果单词是空字符串,则返回指定的值。
下一个目标是创建一个条件,如果 word 已经是复数形式(假设它以 's' 'es' 'ies' .. 等结尾),则该函数返回一个字典具有以下值:**word_in_plural = word 和 x = 'already_in_plural'。所以输入的词保持不变。例如。 (输入:公寓,输出:公寓)
if word ### is already in plural (ending with plural), function returns a dictionary with values; word_in_plural = word and x = 'already_in_plural'
关于如何读取字符串的最后一个字符以实现规则的任何想法?我也很怀疑这个逻辑。
感谢您对 SOF 社区的投入。
【问题讨论】:
标签: python dictionary if-statement conditional-statements