2016-06-13 4 views
0

は、重複を持つ単語の合計数をカウントしようとしていますが、これは{'count': 1, 'words.As': 1, 'said': 1, 'file.\n': 1, 'this': 1, 'text': 1, 'is': 1, 'of': 1, 'some': 1, ',i': 1, 'to': 1, 'only': 1, 'Hi': 1, 'a': 1, 'file': 1, 'recognize': 1, 'test': 1, 'the': 1, 'repeat': 1, 'before': 1} カウント数 - 辞書内包表記を使用して、次のコードでのpython

になり、私は二回isが表示されない、または任意私はここで間違って何をしているのですか?

test_readme.txt

Hi this is some text to recognize the count of words.As said before this is only a test file ,i repeat test file.

with open('test_readme.txt') as f: 
    di = { w : di[w]+1 if w in di else 1 for l in f for w in l.split(' ')} 
print di 
+0

私は 'is'を2回見ます:'は単語の数を認識するためのテキストです。これは ' –

+0

と言われています。あなたのコードから' TypeError:expected character buffer object'例外が発生します。 –

答えて

2

あなたはdiにアクセスすることはできません。代わりに

、単に私がカウンターを使用しますが、文字列全体にCounter

from collections import Counter 

counter = Counter() 
with open('test_readme.txt') as f: 
    for line in f: 
     counter += Counter(line.split()) 
+0

更新を見る... –

2

あなたは辞書内包表記を使用することはできません。 diは作成中に変更されず、すでに辞書を定義していない場合はコードがNameErrorになります。

>>> s = """Hi this is some text to recognize the count of words. 
... As said before this is only a test file ,i repeat test file.""" 
>>> 
>>> di = { w : di[w]+1 if w in di else 1 for l in s.split('\n') for w in l.split(' ')} 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 1, in <dictcomp> 
NameError: global name 'di' is not defined 

あなたがcollectionsモジュールからdefaultdict()またはCounter()使用することができます。

from collections import defaultdict 

di = defaultdict(int) 
with open('test_readme.txt') as f: 
    for line in f: 
     for w in line.strip().split(): 
      di[w]+=1 

デモ:それが移入されている間

>>> for line in s.split('\n'): 
... for w in line.strip().split(): 
...   di[w]+=1 
... 
>>> di 
defaultdict(<type 'int'>, {'count': 1, 'a': 1, 'said': 1, 'words.': 1, 'this': 2, 'text': 1, 'is': 2, 'of': 1, 'some': 1, 'only': 1, ',i': 1, 'to': 1, 'As': 1, 'Hi': 1, 'file': 1, 'recognize': 1, 'test': 2, 'the': 1, 'file.': 1, 'repeat': 1, 'before': 1}) 
>>> 
+0

python cantで参照します。あなたのdiの例の現在のオブジェクトに? – Rajeev

+1

いいえ、いいえ、Pythonの99%は実行時についてです。それは実行時にオブジェクトを定義し、操作の終わりまではPythonは 'di'を作成しません。 – Kasramvd

+0

ありがとう、いい情報....... – Rajeev

1

使用:

from collections import Counter 

with open('readme.txt') as f: 
    s = Counter(f.read().replace('\n', '').split(' ')) 

#Out[8]: Counter({'this': 2, 'is': 2, 'test': 2, 'count': 1, 'words.As': 1, 'said': 1, 'text': 1, 'of': 1, 'some': 1, ',i': 1, 'to': 1, 'only': 1, 'Hi': 1, 'a': 1, 'file': 1, ' 
#recognize': 1, 'the': 1, 'file.': 1, 'repeat': 1, 'before': 1}) 
1

非常に読みやすいソリューションが

サンプルを考慮し
Thedict = {} 
fo = open('sample.txt') 
for line in fo: 
    for word in line.split(' '): 
     word = word.strip('.').strip() 
     if(word in Thedict): 
      Thedict[word] = Thedict[word] + 1 
     else: 
      Thedict[word] = 0 

print(Thedict) 

だろうがテキストを保持する

1

さらに別のCounterソリューションは、Counterへの単一の呼び出しで繰り返し、ネストされたジェネレータ式を使ってファイルを実行します:

from collections import Counter 

with open('test_readme.txt') as f: 
    counts = Counter(word for line in f for word in line.strip().split()) 

、すでに指摘したように、あなたが生成する式で変数にアクセスすることはできません結果、すなわち表現の中間結果を割り当てることができる。式が最初に評価され、結果に対してストアが実行されます。辞書の理解は単一の式であるため、評価され、結果が格納されます。

関連する問題