|
| 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.lower() |
| 25 | +#统一为小写 |
| 26 | +text=text.replace("\n"," ")#换行符不能直接替换为无,否则行末单词会和行首单词相连,影响后续统计 |
| 27 | +text=text.replace("!","") |
| 28 | +text=text.replace(".","") |
| 29 | +text=text.replace(",","") |
| 30 | +text=text.replace("--","") |
| 31 | +text=text.replace("*","") |
| 32 | + #去除换行符和其他符号 |
| 33 | +print(text) |
| 34 | +copy = text.split(' ')#老规矩,先转换为列表,再进行统计 |
| 35 | +a={}#创建空字典 |
| 36 | +for x in copy: |
| 37 | + if x in a: |
| 38 | + a[x]=a[x]+1 |
| 39 | + else: |
| 40 | + a[x]=1 |
| 41 | +for x in sorted(a,key=a.__getitem__,reverse=True): |
| 42 | + print(x,a[x]) |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
0 commit comments