2017-05-19 3 views
2

研究の主題はText processing and detection from a specific dictionary in pythonトピックから取られました。多分私はOPの問題を誤解しましたが、コードを改善しようとしました。だから、おそらく私の質問は少し違うかもしれません。私がやりたいことを説明する前に、私はあなたとコードを共有してみましょう:プレーンテキストから辞書キーを取得するにはどうすればよいですか?

dict_1={"Liquid Biopsy":"Blood for analysis","cfDNA":"Blood for analysis"} 
list_1=[u'Liquid', u'biopsy',u'based', u'on', u'circulating', u'cell-free', u'DNA', u'(cfDNA)', u'analysis', u'are', u'described', u'as', u'surrogate', u'samples', u'for', u'molecular', u'analysis.'] 
for i in dict_1: 
    if i.lower() in " ".join(list_1).lower(): 
     print("Key: {}\nValue: {}\n".format(i,dict_1[i])) 

これらのコードはlist_1に書かれたプレーンテキストから辞書のキーをキャッチすることができます。しかし、私はこのコードで勉強していたときに、何らかの辞書キーがlist_1で繰り返すかどうか疑問に思いました。それから、私はこのlist_1で同じ鍵を2回書いた。そして、上記のコードは繰り返しのものを認識しませんでしたが、プログラムは以下のように同じ結果を出しました。

Key: cfDNA 
Value: Blood for analysis 

Key: Liquid Biopsy 
Value: Blood for analysis 


Process finished with exit code 0 

はその後、私は私の方法を変更しようと以下の通りである別のコードを書いた:

dict_1={"Liquid Biopsy":"Blood for analysis","cfDNA":"Blood for analysis"} 
list_1=[u'Liquid', u'biopsy',u'based', u'on', u'circulating', u'cell-free', "cfdna",u'DNA', u'(cfDNA)', u'analysis', u'are', u'described', u'as', u'surrogate', u'samples', u'for', u'molecular', u'analysis.'] 
for i in list_1: 
    for j in dict_1: 
     for k in j.split(): 
      count=0 
      if k.lower() in i.lower(): 
       count+=1 
       print("Key: {}\nValue: {}\nCount: {}\nDescription: Came from '{}'\n".format(j, dict_1[j],str(count),i)) 

をしかし、それは明らかだった、最後のコードは、望ましくない結果を与えるだろう。以下に見られるように、プログラムはのliquidbiopsyの単語をキャッチします。 cfDNAlist_1で2回書かれたので、プログラムは2回キャッチします。しかし、結果を1回書くことは可能ですが、キャッチ時間を合計しますか?

Key: Liquid Biopsy 
Value: Blood for analysis 
Count: 1 
Description: Came from 'Liquid' 

Key: Liquid Biopsy 
Value: Blood for analysis 
Count: 1 
Description: Came from 'biopsy' 

Key: cfDNA 
Value: Blood for analysis 
Count: 1 
Description: Came from 'cfdna' 

Key: cfDNA 
Value: Blood for analysis 
Count: 1 
Description: Came from '(cfDNA)' 


Process finished with exit code 0 

私がしたいことを理解していただければ幸いです。私はテキストで書かれたすべてのキーをキャッチしたい。また、何度も何度もカウントしたいのですが、これらのキーはテキストで繰り返されます。

+0

あなたはあなたの意見を共有できますか?それがファイルかリストか他であれば? –

+0

これはlist_1というコードで定義されたリストです。 –

答えて

3

私が正しく理解していれば、テキストに「キーワード」が表示された回数を知りたいでしょう。これには "re"モジュールを使うことができます。

import re 

dict_1={"Liquid Biopsy":"Blood for analysis","cfDNA":"Blood for analysis", "asfdafaf":"dunno"} 
list_1=[u'Liquid', u'biopsy',u'based', u'on', u'circulating', u'cell-free', "cfdna",u'DNA', u'(cfDNA)', u'analysis', u'are', u'described', u'as', u'surrogate', u'samples', u'for', u'molecular', u'analysis.'] 

text = ' '.join(list_1).lower() 

for key in dict_1: 
    n = len(re.findall(key.lower(), text)) 
    if n > 0: 
     print('Key:', key) 
     print('Value:', dict_1[key]) 
     print('n:', n) 
     print() 
+0

はい、それは私がやりたいことです。どうもありがとうございました。 –

0

最近、私は「再」モジュールをインポートせずにプレーンテキストで辞書のキーリピートを行う回数をカウントに関する新しい方法を学びました。おそらく、このトピックに別の方法を入れることは適切です。

dict_1={"Liquid Biopsy":"Blood for analysis","cfDNA":"Blood for analysis"} 
list_1=[u'Liquid', u'biopsy', u'liquid', u'biopsy',u'based',u'cfdna' ,u'on', u'circulating', u'cell-free', u'DNA', u'(cfDNA)', u'analysis', u'are', u'described', u'as', u'surrogate', u'samples', u'for', u'molecular', u'analysis.'] 
string_1=" ".join(list_1).lower() 
for i in dict_1: 
    if i.lower() in string_1: 
     print("Key: {}\nValue: {}\nCount: {}\n".format(i,dict_1[i],string_1.count(i.lower()))) 

上記のコードは、reモジュールをインポートする方法とほぼ同じ結果を示します。違いは、キーを2回書いていないことです。したがって、最初の投稿に書かれた最初のコード構造に少し似ています。

Key: Liquid Biopsy 
Value: Blood for analysis 
Count: 2 

Key: cfDNA 
Value: Blood for analysis 
Count: 2 


Process finished with exit code 0 
関連する問題