統計学は、計算科学、経営科学、社会学、数学などの多くの分野の基本的な問題です。関連する問題、方法、テクニックが「統計学」という学問を形成します。
問題の説明は次のとおりです。
Python プログラムを使用して、「ハムレット」の作品内で最も多く出現する単語を数え、除外シソーラスを設定し、一部の冠詞、代名詞、接続詞などを除外します。
ハムレット全集のテキストファイルの一部は次のとおりです。
必要に応じて、ブロガーに連絡して、ハムレットの全作品のテキスト ファイルを入手できます。
プログラムコードは次のとおりです。
excludes = {"the", "and","to","that","his","this","but","of", "you",
"a", "an","i","we","it", "my","me", "in","your","he"}#排除词库
def getText():
txt = open("hamlet.txt", "r").read()
txt = txt.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
txt = txt.replace(ch, " ") # 将文本中特殊字符替换为空格
return txt
hamletTxt = getText()
words = hamletTxt.split()
counts = {}
for word in words:
counts[word] = counts.get(word, 0) + 1
for word in excludes:
del (counts[word])
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
print("Hamlet出现最多的的单词为:")
for i in range(10):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))
プログラムを実行した結果は次のようになります。
これを見た友達は、離れる前に「いいね!」することを忘れないでください。
ブロガーをフォローして、Python プログラミングの知識をさらに学びましょう。