2017-01-09 9 views
-1
fhand = open ("foodtips.txt", 'r') 
fhand1 = open ("foodtips.txt", 'r') 

count = 0 

lines = fhand.readlines() 

fhand_text = fhand1.read() 

fhand_text1 = fhand_text.replace (".", "") 
fhand_text2 = fhand_text1.replace (",", "") 
fhand_text3 = fhand_text2.replace ("'", "") 
fhand_text4 = fhand_text3.replace ("(", " ") 
fhand_text5 = fhand_text4.replace (")", " ") 
fhand_text6 = fhand_text5.replace ("-", " ") 
fhand_text7 = fhand_text6.lower().strip() 
fhand_text8 = fhand_text7.split() 

wordlist = list(set(fhand_text8)) 


for i in range (len(lines)) : 
    appeartimes = [] 
    for word in wordlist : 
     if word in lines [i] : 
      appeartimes.append(i) 
     print word, appeartimes 

こんにちは、皆さん。私はPythonで新しく、現在必須クラスの基礎を学んでいます。しかし、最終試験でゼロ点を得た後、私は解決しようとしているこのコードで何が間違っているのかを尋ねたいと思い、1つの質問で5時間を過ごします。ループとリストのみで単語インデックスを作成する

予想の答えのようなものです:私はこの質問で解決しなければならない詳細な問題としてある

be = [34, 48, 66, 128, 220, ...] 
the = [10, 12, 24, 33, 66, ...] 
as = [1, 55, 88, ...] 

  1. すべての代名詞を削除します。
  2. それは、アプリケーションが非常識行く作り、千数百回のようにループするので、5回以上

すべての結果はおろかを削除し、しかし、私はちょうどでも初期のコードが実行される得ることはありません。

問題を解決するための任意のアイデアは非常に高く評価されます。

+3

あなたは達成しようとしていることを明確に説明していません... –

+0

コードにかなりの誤りがあります(たとえば、関数呼び出しにスペースが含まれていない、つまり 'open(.. '' open(...) 'ではなく) ''( '' lines [i] ''ではなく '' lines [i]' ')にも同じことが言えます。別のオブジェクトなどとして操作を置き換えます。また、あなたが何をしようとしているのか、あなたの例が示しているものが不明です( 'be'、' the'、 'as'の数値は何ですか?) –

答えて

0

私は何かを見つけたと思う。

'Word' [row_number1, row_number2, row_number3] 

が、私はまだ単語リストに正確な単語を見つける方法を見つけることができません:これは私と単語インデックスのリストを与える

wordlist = list(set(fhand_text10)) 
wordlist.sort() 

for word in wordlist : 
    appeartimes = [] 
    count = 0 
    for i in range(len(lines)) : 
     if word in lines [i] : 
      count = count + 1 
     if count > 5 : 
      continue 
     else: 
      appeartimes.append(i+2) 

if 5 > len(appeartimes) > 0 : 
    print word, appeartimes 

。たとえば、「野菜」の中に「有能」と表示され、re.matchやre.searchは単語のリストでは機能しないようです。

+0

私は使用しました 're.search(r '\ b' + word + r '\ b'、行[i])の場合: の代わりに '行内の単語[i]: が機能しました! –

関連する問題