亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在python中使用for循環從字符串中打印每個唯一單詞的頻率

如何在python中使用for循環從字符串中打印每個唯一單詞的頻率

Helenr 2021-07-01 14:01:11
該段落旨在包含空格和隨機標點符號,我通過執行 .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_countdef unique(word):    result = []    for f in word:        if f not in result:            result.append(f)    return resultunique_list = unique(paragraph_list)
查看完整描述

2 回答

?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

請注意,您的示例文本很簡單,但標點規則可能很復雜或未正確遵守。包含 2 個相鄰空格的文本是什么(是的,它不正確但很頻繁)?如果作者更習慣法語并在冒號或分號前后寫空格怎么辦?


我認為's構造需要特殊處理。怎么樣:"""John has a bicycle. Mary says that her one is nicer that John's."""恕我直言,這個詞John在這里出現了兩次,而你的算法會看到 1John和 1 Johns。


此外,由于 Unicode 文本現在在 WEB 頁面上很常見,您應該準備好找到空格和標點符號的高代碼等效項:


“ U+201C LEFT DOUBLE QUOTATION MARK

” U+201D RIGHT DOUBLE QUOTATION MARK

’ U+2019 RIGHT SINGLE QUOTATION MARK

‘ U+2018 LEFT SINGLE QUOTATION MARK

  U+00A0 NO-BREAK SPACE

此外,根據這個較舊的問題,刪除標點符號的最佳方法是translate. 鏈接問題使用 Python 2 語法,但在 Python 3 中您可以執行以下操作:


paragraph = paragraph.strip()                   # remove initial and terminal white spaces

paragraph = paragraph.translate(str.maketrans('“”’‘\xa0', '""\'\' '))  # fix high code punctuations

paragraph = re.replace("\w's\s", "", paragraph)  # remove 's

paragraph = paragraph.translate(str.maketrans(None, None, string.punctuation) # remove punctuations

words = paragraph.split()


查看完整回答
反對 回復 2021-07-13
  • 2 回答
  • 0 關注
  • 251 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號