|
| 1 | +text = ''' |
| 2 | +The Zen of Python, by Tim Peters |
| 3 | +Beautiful is better than ugly. |
| 4 | +Explicit is better than implicit. |
| 5 | +Simple is better than complex. |
| 6 | +Complex is better than complicated. |
| 7 | +Flat is better than nested. |
| 8 | +Sparse is better than dense. |
| 9 | +Readability counts. |
| 10 | +Special cases aren't special enough to break the rules. |
| 11 | +Although practicality beats purity. |
| 12 | +Errors should never pass silently. |
| 13 | +Unless explicitly silenced. |
| 14 | +In the face of ambxiguity, refuse the temptation to guess. |
| 15 | +There should be one-- and preferably only one --obvious way to do |
| 16 | +it. |
| 17 | +Although that way may not be obvious at first unless you're Dutch. |
| 18 | +Now is better than never. |
| 19 | +Although never is often better than *right* now. |
| 20 | +If the implementation is hard to explain, it's a bad idea. |
| 21 | +If the implementation is easy to explain, it may be a good idea. |
| 22 | +Namespaces are one honking great idea -- let's do more of those! |
| 23 | +'''#要处理的文本 |
| 24 | +text = text. replace ("better","worse")#用replace函数进行关键词的替换 |
| 25 | +text = text. replace ("\n"," ")#去除换行符,使结果更美观 |
| 26 | +text = text. replace ("*","") |
| 27 | +text = text. replace ("--","") |
| 28 | +text = text. replace (".","")#去除其他符号,否则会影响后续统计 |
| 29 | +copy = text.split(' ')#将字符串转化为列表,便于之后的操作 |
| 30 | +copy = [x for x in copy if x != '']#去除列表中的空格元素,否则总是有空格会参与排序 |
| 31 | +copy = [x for x in copy if "ea" not in x]#找出copy中的含“ea”的元素并删去,用循环加条件判断会有漏网之鱼,原因不明 |
| 32 | +text = ' '.join(copy)#由于大小写转换命令只适用于字符串,故转换回字符串 |
| 33 | +text=text.swapcase()#进行大小写互换 |
| 34 | +copy = text.split(' ')#为了排序,在不更改任务顺序的前提下再次转化为列表进行排序 |
| 35 | +copy.sort() |
| 36 | +print(copy) |
| 37 | + |
0 commit comments