2016-09-16 2 views
2

はのは、私はキーワードのリストを持っているとしましょう:リストの複数の項目が文字列内に表示されているかどうかを確認する方法は?

keywords = ["history terms","history words","history vocab","history words terms","history vocab words","science list","science terms vocab","math terms words vocab"] 

そしてメイン用語のリスト:

私が作っているスクリプトを:より明確に問題を状態に更新

`main_terms = ["terms","words","vocab","list"]` 

長いキーワードリストから重複したものを削除することです。私は、スペルミスや多少の変種(例えば、ヒットソリ用語、歴史用語)を取り除くことができました。

私の問題私がこのキーワードのリストで探している用語は複数ありますが、これらの用語の1つをキーワード(例:「歴史用語」)で見つけたら、異なる用語または用語の組み合わせ(例えば、「歴史語彙」、「歴史語」、「歴史語用語」など)を除いて同一であることは、重複しているとみなされるべきである。限り、用語の小さい数を持つために、同一の保存されたキーワード(例がないとして

  • (例:「数学の用語の単語の単語」)キーワードに複数の用語を持ってOKです「数学用語」または理想的には「数学語彙」のような単一の用語)。
+0

keywords = ["history terms", "history words", "history vocab", "history words terms", "history vocab words", "science list", "science terms vocab", "math terms words vocab"] main_terms = {"terms","words","vocab","list"} result = {} for words in keywords: s = set(words.split()) s_subject = s - main_terms subject = s_subject and next(iter(s_subject)) if s | main_terms and subject and subject not in result: result[subject] = words 

リストに結果値をオンにしますhttp://stackoverflow.com/questions/3931541/python-check-if-all-of-the-following-iリスト内のリスト – drum

+0

@drum - その質問は当てはまらないようです。 – TigerhawkT3

+1

'' main_termsの中に複数のキーワードがあり、 ''数学用語を出力しているキーワードを削除する3つの単語を含む単語vocabは私には意味がありません。 – TemporalWolf

答えて

1

ループ上のデモを参照して確認してください。

>>> list(result.values()) 
['math terms words vocab', 'history terms', 'science list'] 
+0

これは実際に私の問題を解決すると思います!ありがとう! main_termsの1つを他のものの上にランク付けする方法はありますか?現在のところ、「履歴用語」が「履歴用語集」の前にある場合、後者は削除されますが、「用語」を含むすべてのキーワードを同一のキーワードで維持したいのであれば、どうでしょうか? ? – DukeSilver

+0

@DukeSilver - 'if'を' if s | 'に変更すると、 main_termsとsubjectとsubjectに結果や ':'の中に 'terms'がない場合、それは '' science ': 'science'の代わりに' 'science terms '' vocab'''を' 'science ''(この例では)保存します。それはあなたが念頭に置いたことですか? – TigerhawkT3

+0

(私は上記のコメントを間違えた、すみません!)。私は元のキーワードリストになかったものを返そうとはしません。問題は、それが見つけた最初のmain_termインスタンスを保持し、残りの部分を削除することですが、main_termsを「ランク付け」するのが大好きです。例えば、 'history list'を探しますが、それがキーワードでなければ、' history vocab'を探します。 'history vocab'はキーワードリストにあるので、それを保持して他のものを削除します('歴史用語 '、'歴史語 'など)。それは意味をなさないでしょうか? – DukeSilver

0

私はよりエレガントな解決策があると確信しているが、これはあなたが探しているいる解決策になるようだ、少なくとも部分1用):

>>> def remove_main_terms(keyword): 
     words = keyword.split() 
     count = 0 
     to_keep = [] 
     for word in words: 
      if word in main_terms: 
       count += 1 
      if count < 2: 
       to_keep.append(word) 
      else: 
       pass 
     return " ".join(to_keep) 

>>> keywords = ["history terms","history words","history vocab","history words terms","history vocab words","science list","science terms vocab","math terms words vocab"] 

>>> main_terms = ["terms","words","vocab","list"] 

>>> new_list = [] 
>>> for w in keywords: 
     new_list.append(remove_main_terms(w)) 

>>> new_list 
['history terms', 'history words', 'history vocab', 'history words', 'history vocab', 'science list', 'science terms', 'math terms'] 
0

は編集:私はますますよXY Questionと尋ねていると思っていて、あなたはユニークな主題を望んでいます。

その場合は、次のようにしても良い作品:

result = [] 
found = [] 
for word in keywords: 
    for term in main_terms: 
     if term in word: 
      word = word.replace(term, "") 
    result.append(word.strip()) 

print set(result) 

set(['science', 'math', 'history'])


これは、同じ結果を使用して元の問題を解決するが、後に条件を無視してそれをしない出力最初の唯一の合格する最初の単語。

result = [] 
found = [] 
for word in keywords: 
    found = False 
    for res in result: 
     if word.split()[0] in res: 
      found = True 
    if not found: 
     result.append(word) 
print result 

main_termsに対して各1 keywordsを通じてrepl.it

関連する問題