plural1.py源代码分析

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

"""Pluralize English nouns (stage 1)

This program is part of "Dive Into Python", a free Python book for

experienced programmers. Visit http://diveintopython.org/ for the

latest version.

Command line usage:

$ python plural1.py noun

nouns

"""

__author__ = "Mark Pilgrim ([email protected])"

__version__ = "$Revision: 1.3 $"

__date__ = "$Date: 2004/03/18 16:43:37 $"

__copyright__ = "Copyright (c) 2004 Mark Pilgrim"

__license__ = "Python"

import re

def plural(noun):

if re.search('[sxz]$', noun):

return re.sub('$', 'es', noun)

#如果以s、x、z中的任意一个结尾,则直接在末尾加es变为复数

elif re.search('[^aeioudgkprt]h$', noun):

return re.sub('$', 'es', noun)

#如果以除aeioudgkprt中的任意一个字母和h结尾,则也加es变为复数

elif re.search('[^aeiou]y$', noun):

return re.sub('y$', 'ies', noun)

#如果以除aeiou中的任意一个字母和y结尾,则将结尾的y去掉,然后加ies变为复数

else:

return noun + 's'

#其他的所有情况都是在单词末尾加s变为复数

if __name__ == '__main__':

import sys

#导入sys模块

if sys.argv[1:]:

print plural(sys.argv[1])

打印第一个参数的复数形式

else:

print __doc__

你可能感兴趣的:(C++,c,python,C#)