2016-08-27 10 views
0

の私の第二の機能の私の最初の関数から辞書を呼び出す方法を、私は私は2つの段階に分けたいプログラム、構築しています:私は、Pythonにかなり新しいですし、新しい学習に役立つPythonの

ステップを1)テキストファイル内の特定の単語の数を数え、キーと値のペアが{word、count}である辞書に格納する。

ステップ2)(1)から最初の100単語を表示

ステップ1はうまくいきますが、ステップ2を試してみると、最初の関数から辞書を呼び出すのに苦労しています。私は新しい変数 'tallies'を作成しますが、これはタプルであり、辞書の最初のエントリのみを表示します。

完全な辞書を2番目の関数に呼び出すにはどうすればよいですか?

ありがとうございました。

filename = 'nameoffile.txt' 

def tally(): 
    file = open(filename,'r') 
    wordcount={} 
    for word in file.read().split(): 
    if word not in wordcount: 
     wordcount[word] = 1 
    else: 
     wordcount[word] += 1 
    for k,v in wordcount.items(): 
    return k,v 

def Count(): 
    tallies = tally() 
    print tallies 

Count() 
+0

あなたはコードを書く前に、あなたはPython Googleの新人です。既にそこに解決策があるかもしれません。 Pythonは慣用句です。 Pplは同じコードを使用します。 'Counter'を使用する答えを見てください。 – Merlin

答えて

0

これらのタスクはまさに​​collections.Counter()の対象です。この関数を使用すると、単語とその頻度を含む逆辞書オブジェクトを作成できます。分割されたテキストで呼び出すことができます。その後、Counter.most_common(N)を使用して、N個の共通アイテムを取得します。

一部を次のようにあなたのコードについて:

for k,v in wordcount.items(): 
    return k,v 

最初の反復の後、あなたはreturnによりループを壊しているとそれだけで最初の項目を返します。

あなたは、単に辞書を返すことができます。

def tally(): 
    file = open(filename,'r') 
    wordcount={} 
    for word in file.read().split(): 
     if word not in wordcount: 
      wordcount[word] = 1 
     else: 
      wordcount[word] += 1 
    return wordcount 

あなたも手動カウンタオブジェクトを作成するためにcollections.defaultdict()を使用することができます。この関数を使用する利点は、1つのメソッドをオーバーライドし、書き込み可能なインスタンス変数を1つ追加することです。

from collections import defaultdict 

wordcount = defaultdict(int) # default is 0 

def tally(): 
    with open(filename) as f 
    for word in f.read().split(): 
      wordcount[word] += 1 
    return wordcount 

そして、あなたはその種に2番目の項目によってアイテムを言って、それにキー機能を渡すことで辞書項目にsorted()機能を使用することができ、ソート項目を返します。たとえば、次のように

sorted(wordcount.items(), key=lambda x:x[1]) 

しかし、私が最初に言ったように、ニシキヘビと最適化されたアプローチがcollections. Counter()を使用しています。

from collections import Counter 

with open(filename) as f: 
    wordcount = Counter(f.read().split()) 

top100 = wordcount.most_common(100) 
1

あなたの集計関数は、最初に表示される項目を返します。 returnは一度だけ返すことができますが、ループで呼び出すことになります。全体の語数の辞書を戻してください:順番に、それはあなたがリストにそのタプルをソートする必要があるので、Pythonで

filename = 'nameoffile.txt' 

def tally(): 
    file = open(filename,'r') 
    wordcount={} 
    for word in file.read().split(): 
    if word not in wordcount: 
     wordcount[word] = 1 
    else: 
     wordcount[word] += 1 
    return wordcount 

def Count(): 
    tallies = tally() 
    sorted_tallies = sorted(tallies.items(), key=operator.itemgetter(1)) 
    print sorted_tallies[:100] 

Count() 

dictは、自然が順不同です。コードsortedがこれを行います(see this reference)。

幸運!

+0

ご協力いただきありがとうございます!これは本当に役に立ちました! – denimskies

0

あなたの問題は、初めてのアイテムをつかんだ最初の繰り返しの後にk,vを返したことです。次のコードはこれを修正します。私はまた、反転機能を追加しました。

def tally(): 
    file = open(filename,'r') 
    wordcount={} 
    for word in file.read().split(): 
     if word not in wordcount: 
      wordcount[word] = 1 
     else: 
      wordcount[word] += 1 

    return tuple(reversed(sorted(((k, v) for k, v in wordcount.items()),key=lambda x: x[1]))) 

def Count(): 
    tallies = tally() 
    print tallies 
関連する問題