-
Notifications
You must be signed in to change notification settings - Fork 879
自学训练营学习7群 day6作业 #5949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
自学训练营学习7群 day6作业 #5949
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| en_text = ''' | ||
| The Zen of Python, by Tim Peters | ||
| Beautiful is better than ugly. | ||
| Explicit is better than implicit. | ||
| Simple is better than complex. | ||
| Complex is better than complicated. | ||
| Flat is better than nested. | ||
| Sparse is better than dense. | ||
| Readability counts. | ||
| Special cases aren't special enough to break the rules. | ||
| Although practicality beats purity. | ||
| Errors should never pass silently. | ||
| Unless explicitly silenced. | ||
| In the face of ambxiguity, refuse the temptation to guess. | ||
| There should be one-- and preferably only one --obvious way to do | ||
| it. | ||
| Although that way may not be obvious at first unless you're Dutch. | ||
| Now is better than never. | ||
| Although never is often better than *right* now. | ||
| If the implementation is hard to explain, it's a bad idea. | ||
| If the implementation is easy to explain, it may be a good idea. | ||
| Namespaces are one honking great idea -- let's do more of those! | ||
| ''' | ||
|
|
||
|
|
||
| def stats_text_en(text): | ||
| elements = text.split() | ||
| words = [] | ||
| symbols = ',.*-!' | ||
| for element in elements: | ||
| for symbol in symbols: | ||
| element = element.replace(symbol, '') | ||
| if len(element): | ||
| words.append(element) | ||
| counter = {} | ||
| word_set = set(words) | ||
|
|
||
|
|
||
|
|
||
| for word in word_set: | ||
| counter[word] = words.count(word) | ||
| return sorted(counter.items(), key=lambda x: x[1], reverse=True) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| def stats_text_cn(text): | ||
| cn_characters = [] | ||
| for character in text: | ||
| if '\u4e00' <= character <= '\u9fff': | ||
| cn_characters.append(character) | ||
| counter = {} | ||
| cn_character_set = set(cn_characters) | ||
| for character in cn_character_set: | ||
| counter[character] = cn_characters.count(character) | ||
| return sorted(counter.items(), key=lambda x: x[1], reverse=True) | ||
|
|
||
|
|
||
|
|
||
| cn_text = ''' | ||
| Python之禅 by Tim Peters | ||
|
|
||
| 优美胜于丑陋 | ||
| 明了胜于晦涩 | ||
| 简洁胜于复杂 | ||
| 复杂胜于凌乱 | ||
| 扁平胜于嵌套 | ||
| 间隔胜于紧凑 | ||
| 可读性很重要 | ||
| 即便假借特例的实用性之名,也不可违背这些规则 | ||
| 不要包容所有错误,除非你确定需要这样做 | ||
| 当存在多种可能,不要尝试去猜测 | ||
| 而是尽量找一种,最好是唯一一种明显的解决方案 | ||
| 虽然这并不容易,因为你不是 Python 之父 | ||
| 做也许好过不做,但不假思索就动手还不如不做 | ||
| 。。。 | ||
| ''' | ||
|
|
||
| if __name__ == '__main__': | ||
| en_result = stats_text_en(en_text) | ||
| cn_result = stats_text_en(cn_text) | ||
| print('统计参数中每个英语单词出现的次数 ==>\n',en_result) | ||
| print('统计参数中每个中文汉字出现的次数 ==>\n',cn_result) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里函数的定义要缩进到行首