该段落旨在包含空格和随机标点符号,我通过执行 .replace 在 for 循环中删除了它们。然后我通过 .split() 将段落放入一个列表中以获得 ['the', 'title', 'etc']。然后我做了两个函数 count words 来计算每个单词,但我不想它计算每个单词,所以我做了另一个函数来创建一个唯一的列表。但是,我需要创建一个 for 循环来打印出每个单词以及它被说了多少次,输出是这样的
The word The appears 2 times in the paragraph.
The word titled appears 1 times in the paragraph.
The word track appears 1 times in the paragraph.
我也很难理解 for 循环的本质。我读到我们应该只使用 for 循环进行计数,而 while 循环可以用于其他任何事情,但 while 循环也可以用于计数。
paragraph = """ The titled track “Heart Attack” does not interpret the
feelings of being in love in a serious way,
but with Chuu’s own adorable emoticon like ways. The music video has
references to historical and fictional
figures such as the artist Rene Magritte!!.... """
for r in ((",", ""), ("!", ""), (".", ""), (" ", "")):
paragraph = paragraph.replace(*r)
paragraph_list = paragraph.split()
def count_words(word, word_list):
word_count = 0
for i in range(len(word_list)):
if word_list[i] == word:
word_count += 1
return word_count
def unique(word):
result = []
for f in word:
if f not in result:
result.append(f)
return result
unique_list = unique(paragraph_list)
慕侠2389804
相关分类