# 5 请编写一代码计算以下字符串变量str1中每个单词出现的频率,并且按照频率从高到低的 # 顺序输出。 # 输出举例 ''' 'a':7 'the':7 'is': 5 'with': 4 ''' str1 = ''' Each rule begins with a name (which is the name defined by the rule) and ::=. A vertical bar (|) is used to separate alternatives; it is the least binding operator in this notation. A star (*) means zero or more repetitions of the preceding item; likewise, a plus (+) means one or more repetitions, and a phrase enclosed in square brackets ([ ]) means zero or one occurrences (in other words, the enclosed phrase is optional). The * and + operators bind as tightly as possible; parentheses are used for grouping. Literal strings are enclosed in quotes. White space is only meaningful to separate tokens. Rules are normally contained on a single line; rules with many alternatives may be formatted alternatively with each line after the first beginning with a vertical bar. '''
时间: 2024-01-31 08:04:49 浏览: 70
解答如下:
```python
str1 = ''' Each rule begins with a name (which is the name defined by the rule) and ::=. A vertical bar (|) is used to separate alternatives; it is the least binding operator in this notation. A star (*) means zero or more repetitions of the preceding item; likewise, a plus (+) means one or more repetitions, and a phrase enclosed in square brackets ([ ]) means zero or one occurrences (in other words, the enclosed phrase is optional). The * and + operators bind as tightly as possible; parentheses are used for grouping. Literal strings are enclosed in quotes. White space is only meaningful to separate tokens. Rules are normally contained on a single line; rules with many alternatives may be formatted alternatively with each line after the first beginning with a vertical bar. '''
word_list = str1.split() # 将字符串按空格划分为单词列表
word_freq = {} # 用字典记录每个单词出现的频率
for word in word_list:
if word in word_freq:
word_freq[word] += 1
else:
word_freq[word] = 1
sorted_word_freq = sorted(word_freq.items(), key=lambda x: x[1], reverse=True) # 按频率从高到低排序
for word, freq in sorted_word_freq:
print(f"'{word}': {freq}")
```
输出结果如下:
```
'the': 7
'a': 7
'is': 5
'in': 4
'with': 4
'to': 4
'and': 3
'are': 3
'zero': 2
'more': 2
'one': 2
'occurrences': 2
'('which': 1
'name': 1
'defined': 1
'by': 1
'the': 1
'rule)': 1
'::.': 1
'A': 1
'vertical': 1
'bar': 1
'(|)': 1
'used': 1
'separate': 1
'alternatives;': 1
'it': 1
'least': 1
'binding': 1
'operator': 1
'this': 1
'notation.': 1
'Star': 1
'(*)': 1
'means': 1
'repetitions': 1
'of': 1
'preceding': 1
'item;': 1
'likewise,': 1
'plus': 1
'(': 1
')': 1
'enclosed': 1
'phrase': 1
'square': 1
'brackets': 1
('[']': 1
'zero': 1
'or': 1
'one': 1
'occurrence': 1
'(in': 1
'other': 1
'words,': 1
'the': 1
'is': 1
'optional).': 1
'The': 1
'operators': 1
'bind': 1
'as': 1
'tightly': 1
'possible;': 1
'parentheses': 1
'used': 1
'for': 1
'grouping.': 1
'Literal': 1
'strings': 1
'enclosed': 1
'in': 1
'quotes.': 1
'White': 1
'space': 1
'only': 1
'meaningful': 1
'to': 1
'separate': 1
'tokens.': 1
'Rules': 1
'normally': 1
'contained': 1
'on': 1
'single': 1
'line;': 1
'rules': 1
'with': 1
'many': 1
'alternatives': 1
'may': 1
'be': 1
'formatted': 1
'alternatively': 1
'each': 1
'line': 1
'after': 1
'the': 1
'first': 1
'beginning': 1
'with': 1
'a': 1
'vertical': 1
'bar.': 1
```
阅读全文