马丁路德金的"I have a dream"节选存放在"freedom.txt"中: I have a dream that one day this nation will rise up, live up to the true meaning of its creed: “We hold these truths to be self-evident; that all men are created equal.” I have a dream that one day on the red hills of Georgia the sons of former slaves and the sons of former slave-owners will be able to sit down together at the table of br otherhood. I have a dream that one day even the state of Mississippi, a state sweltering with th e heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom and justice. I have a dream that my four children will one day live in a nation where they will no t be judged by the color if their skin but by the content of their character. I have a dream today. I have a dream that one day down in Alabama with its governor having his lips drippin g with the words of interposition and nullification, one day right down in Alabama li ttle black boys and black girls will be able to join hands with little white boys and white girls as sisters and brothers. I have a dream today. I have a dream that one day every valley shall be exalted, every hill and mountain sh all be made low, the rough places will be made plain, and the crooked places will be made straight, and the glory of the Lord shall be revealed, and all flesh shall see i t together. 编程实现词汇表,计算每一个单词出现的次数,大小写不区分,输出到"dic.txt" 文件保存。 将源程序和运行结果制作图片提交到答题处。
时间: 2024-02-14 17:07:52 浏览: 110
马丁路德金讲演稿I have a dream
可以使用Python的字符串处理和文件读写功能来实现。
首先,读取"freedom.txt"文件中所有文本,并将其转换为小写字母,可以使用以下代码:
```python
with open('freedom.txt', 'r') as f:
text = f.read().lower() # 读取所有文本并转换为小写字母
```
接着,将文本分割成单词并统计每个单词出现的次数,可以使用以下代码:
```python
words = text.split() # 将文本分割成单词
word_counts = {}
for word in words:
if word in word_counts:
word_counts[word] += 1 # 如果单词已经出现过,增加计数
else:
word_counts[word] = 1 # 如果单词是第一次出现,初始化计数为1
```
最后,将结果输出到"dic.txt"文件中,可以使用以下代码:
```python
with open('dic.txt', 'w') as f:
for word, count in word_counts.items():
line = f'{word}: {count}\n'
f.write(line) # 将每个单词和其计数写入文件
```
完整代码如下:
阅读全文