列表中的Python字符串

2024-10-03 02:34:22 发布

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

我似乎没有发现有人问这个问题,但如果它已经在那里,那么我道歉,并将感谢链接。你知道吗

为了这个问题。 当前正在使用此列表:

nouns = ['house','bee','ducks','blouse','cars']

我想做一个程序,把复数的单词改成单数,单数的单词改成复数。我打算尝试使用索引号来更改列表,例如:

for index, word in enumerate(nouns):
if word[-1] is 'e':
  print nouns[index]==word[-1]+'s'
    print nouns

我对Python还是很陌生,但现在已经卡住了。任何帮助或暗示都将不胜感激。你知道吗


Tags: 程序列表index链接单词carswordhouse
2条回答
nouns = ['house','bee','ducks','blouse','cars']
for index, word in enumerate(nouns):
    if word[-1] == 'e':
        # if this word ends with e, add s
        nouns[index] = word+'s'
    elif word[-1] == 's':
        # if this word ends with s, remove last char
        nouns[index] = word[:-1]
print nouns

请注意,这适用于您提供的特定列表,但由于men等复数形式,因此不能授予其他单词集合

这个怎么样

nouns = ['house','bee','ducks','blouse','cars']

plurals = [x + 's' if x.endswith('e') else x for x in nouns]

相关问题 更多 >