あなたのコードは実行可能な状態に非常に近いです。
P1:あなたのコードでは、単語以外の文字は考慮されません。たとえば、word;
,word.
、およびword
はすべて一意の単語として扱われます。
text = handle.read()
words = text.split()
P2:あなたが代わりにcounts
で独自のリストで、重複が含まれた単語の全体リスト、を反復。もちろん、各単語を複数回印刷することになります。
for word in words:
P3:あなたはファイルを開くが、それを閉じることはありません。あなたのコードには問題はありませんが、改善すべき点があります。このため、ファイルを閉じるために、with open(...):
の構文を使用することが一般的に推奨されています。
handle = open(name, 'r')
ここではいくつかの修正をあなたのコードです:このプログラムで実行した場合
#!/usr/bin/python
import re
name = raw_input('Enter file:')
def percent(part, whole):
return 100 * float(part)/float(whole)
# better way to open files, handles closing the file
with open(name, 'r') as handle:
text = handle.read()
words = text.split()
# get rid of non-word characters that are messing up count
formatted = []
for w in words:
formatted.extend(re.findall(r'\w+', w))
total = len(formatted)
counts = dict()
for word in formatted:
counts[word] = counts.get(word,0) + 1
print "\n"
print"Total Words\n", total
print"\n"
# iterate over the counts dict instead of the original word list
# this way each word is only printed once
for word,count in counts.iteritems():
print word, percent(counts[word],total),"%"
出力:
Total Words
79
text 2.53164556962 %
float 2.53164556962 %
as 1.26582278481 %
file 1.26582278481 %
in 3.79746835443 %
handle 2.53164556962 %
counts 6.32911392405 %
total 3.79746835443 %
open 1.26582278481 %
findall 1.26582278481 %
for 3.79746835443 %
0 1.26582278481 %
percent 2.53164556962 %
formatted 5.06329113924 %
1 1.26582278481 %
re 2.53164556962 %
dict 1.26582278481 %
usr 1.26582278481 %
Words 1.26582278481 %
print 5.06329113924 %
import 1.26582278481 %
split 1.26582278481 %
bin 1.26582278481 %
return 1.26582278481 %
extend 1.26582278481 %
get 1.26582278481 %
python 1.26582278481 %
len 1.26582278481 %
iteritems 1.26582278481 %
part 2.53164556962 %
words 2.53164556962 %
Enter 1.26582278481 %
100 1.26582278481 %
with 1.26582278481 %
count 1.26582278481 %
word 7.59493670886 %
name 2.53164556962 %
read 1.26582278481 %
raw_input 1.26582278481 %
n 3.79746835443 %
r 1.26582278481 %
w 3.79746835443 %
Total 1.26582278481 %
whole 2.53164556962 %
def 1.26582278481 %
編集 -
をフォーマットする単語の追加説明formatted.extend(re.findall(r'\w+', w))
の内訳:
1:リストのextend
の機能がリストを取り、それを指定されたリストに追加します。たとえば:
listA = [1,2,3]
listB = [4,5,6]
listA.extend(listB)
print(listA)
# [1, 2, 3, 4, 5, 6]
2:re.findall(r'\w+', w))
この式は、私たちが気に文字列の一部だけを抽出するためにregular expressionsを使用しています。ここには、Pythonの正規表現のtutorialがあります。
基本的にre.findall(x, y)
は、に記載されている正規表現パターンに一致するy
のすべての部分文字列のリストを返します。ここでは、\w
はすべての単語文字(英数字)を意味し、+
は前述のパターンの1つ以上を意味します。だから一緒に、\w+
は、1つ以上の単語の文字を意味します。
w
で検索している文字列変数に名前をつけると多少混乱しましたが、パターン内の\w
は文字列の変数w
とは関係ありません。
word = 'heres some1; called s0mething!'
re.findall(r'\w+', word)
# ['heres', 'some1', 'called', 's0mething']
「dict」ではなく「collections.Counter」を使用する方がよいでしょう。そうであるように 'counts = Counter(words)'。 'dict'を使って' for'ループよりもはるかに高速で読みやすくなります。 – Arnial
ようこそスタックオーバーフロー。将来、より良い質問をするための小さなメモは、タイトルの問題の簡単な説明を与えることです。例えば、この投稿の場合、より良いタイトルは「単語ごとの単語を印刷する」でしょう。これにより、将来的に自分の質問に対する回答を簡単に検索できるようになります。また、回答しようとしている人があなたの質問を一目で確認できるようになります。それ以外は、これは本当に良い質問でした。あなたは私が最初に試してマークダウンを使用して見た最初の人々の一つです。 –