2016-12-21 4 views
0

私はPythonの初心者で、問題の内容を自分のコードで把握するのに苦労しています。Pythonのリストの値を集計する

私がここでやろうとしているのは、テキストをリストのタプルに変換してから、リスト内のDTの数を数えることです。

のは、txtファイルの最初の3行以下になりますと言ってみましょう:

The/DT Fulton/NNP County/NNP Grand/NNP Jury/NNP said/VBD Friday/NNP an/DT investigation/NN of/IN Atlanta/NNP 's/POS recent/JJ primary/JJ election/NN produced/VBD ``/`` no/DT evidence/NN ''/'' that/IN any/DT irregularities/NNS took/VBD place/NN ./. 
The/DT jury/NN further/RB said/VBD in/IN term-end/JJ presentments/NNS that/IN the/DT City/NNP Executive/NNP Committee/NNP ,/, which/WDT had/VBD over-all/JJ charge/NN of/IN the/DT election/NN ,/, ``/`` deserves/VBZ the/DT praise/NN and/CC thanks/NNS of/IN the/DT City/NNP of/IN Atlanta/NNP ''/'' for/IN the/DT manner/NN in/IN which/WDT the/DT election/NN was/VBD conducted/VBN ./. 
The/DT September-October/NNP term/NN jury/NN had/VBD been/VBN charged/VBN by/IN Fulton/NNP Superior/NNP Court/NNP Judge/NNP Durwood/NNP Pye/NNP to/TO investigate/VB reports/NNS of/IN possible/JJ ``/`` irregularities/NNS ''/'' in/IN the/DT hard-fought/JJ primary/NN which/WDT was/VBD won/VBN by/IN Mayor-nominate/NNP Ivan/NNP Allen/NNP Jr./NNP ./. 

をこれは、ワークスペースの「practice.txt」として保存されました。

with open("practice.txt") as myfile: 
    for line in myfile: 
     cnt += 1 
     word = line.split() 
     total_word_per_line += len(word) 
     total_type_of_words += len(set(word)) 
     a = [tuple(i.split('/')) for i in word] 

    for x in a: 
     DT_sum = 0 
     if x[1] == 'DT': 
      DT_sum += 1 

     total_DT_sum += DT_sum 

    print total_DT_sum 

をしかし出力は、それが唯一の第三のリストでのDTを数え意味total_DT_sum、ため2を示しています

だから私のコードは以下のように見えます。すべてのDTを数えるための提案はありますか?

所望の出力は、予め5(上記の3つの文章からのDTの総数)

おかげです!

+0

あなたは各反復の開始時に0に 'DT_sum'を設定します。その初期化をループの前に移動する必要があります。 –

+1

私はちょうど推測していますが、 'DT_sum = 0'は' for x in a: '行の前にあるべきではありませんか? – fredtantini

+0

あなたの3つの文には13個の '/ DT'文字列があります。 –

答えて

0

、あなたはこのようなfilter()を使用することができます。

my_list = [] 

with open('practice.txt', 'r') as f: 
    for line in f: 
     my_list.extend([tuple(i.split('/')) for i in line.split()]) 

res = filter(lambda i: i[1] == 'DT', my_list) 
print(len(res)) # Output: 13 

extend()my_list

に各ラインの構築タプルを追加するために使用されます

filter()は、'DT'の項目のみを2番目の位置に返します。

出力:

>>> res = filter(lambda i: i[1] == 'DT', my_list) 
>>> res 
[('The', 'DT'), ('an', 'DT'), ('no', 'DT'), ('any', 'DT'), ('The', 'DT'), ('the', 'DT'), ('the', 'DT'), ('the', 'DT'), ('the', 'DT'), ('the', 'DT'), ('the', 'DT'), ('The', 'DT'), ('the', 'DT')] 
>>> 
>>> len(res) 
13 
+0

ありがとうございます! – jay

0

あなたのエラー:

for x in a: 
    DT_sum = 0 

DT_sumが0にリセットされるたびに...あなたは最初からそれをしたい場合には

、それを行うための最も簡単な方法は、のsumとなります各ライン上count

with open("practice.txt") as myfile: 
    nb_dt = sum(line.count("/DT") for line in my_file) 

結果は、あなたが述べたようにしない5、13である

を(手動で検証することができます)

解決策は単語分割を考慮しません。それはある場合はそれも/DTXXXを見つけることを意味します。

だから、もう少し複雑なコードがないこと:/に応じてそれぞれの時間分割のため

with open("practice.txt") as myfile: 
    nb_dt = sum(1 if word.partition("/")[2]=="DT" else 0 for line in my_file for word in line.split()) 

カウント1は、各ラインの各単語について、その右側にDTを持っています。それは'DT'の数をカウントする前に、タプルのリストにデータを保存するために必要な場合