2017-10-07 2 views
-1

にリストの2つのリストを比較私はLST2の各リストにLST1に各リストを比較したいのpython

lst1=[['hey','jude' ,'fox'],['how','you','do']] 
lst2=[['hey','paul'],['how','do']] 

2つのリストを持っています。それらの単語の共通語数が1以上の場合は、大きいリストを新しいリストに追加する必要があります。彼らが共通の単語<を持っているなら、私は同じ新しいリストに両方のリストを追加する必要があります。
例:['hey', 'jude','fox']は、1回繰り返す共通語句heyを持つため、['hey','paul']と比較されます。大きなリスト['hey', 'jude','fox']を新しいリストに追加する必要があります。ここで

+0

希望の出力を表示してください。 –

+0

何を試しましたか? [最小限の完全かつ検証可能なサンプルを作成する方法](https://stackoverflow.com/help/mcve)を参照してください。 –

答えて

0

あなたのソリューションは、次のとおりです。

lst1=[['hey','jude' ,'fox'],['how','you','do']] 
lst2=[['hey','paul'],['how','do']] 
    new_list=[] 

    for item in lst1: 
     for item_1 in lst2: 
      if isinstance(item_1,list): 
       for sub_item in item_1: 
        if sub_item in item: 
         if len(sub_item)>=1: 
          if len(item)>len(item_1): 
           if item not in new_list: 
            new_list.append(item) 
          elif len(item_1)>len(item): 
           if item_1 not in new_list: 
            new_list.append(item_1) 


         if len(sub_item)<2: 
          if sub_item not in new_list: 
           new_list.append(sub_item) 
           new_list.append(item) 



    print([j for i,j in enumerate(new_list) if j not in new_list[:i]]) 

P.S:あなたは 「より大きいか1に等しい言葉」によって何を意味するか、混乱と疑問を述べましたあなたは言葉の長さや言葉のマッチングを意味しますか?私は単語のlenを仮定した。

テスト:あなたのリストを持つ

出力:

[['hey', 'jude', 'fox'], ['how', 'you', 'do']] 

リスト1は、その後comman未満2 lenの手紙を持っている:

lst1=[['hey','jude' ,'fox'],['how','you','do'],["wow"],["wllla","wlahi","aasma"],["h","i","j"]] 
lst2=[['hey','paul'],['how','do'],["wllla"],["h"]] 

出力:

[['hey', 'jude', 'fox'], ['how', 'you', 'do'], ['wllla', 'wlahi', 'aasma'], ['h', 'i', 'j'], 'h'] 

第二のリストは、両方で一致する最大の共通リストの言葉があります:あなたは、ネスト休憩にロジックを避けたい場合は

[['hey', 'jude', 'fox'], ['how', 'you', 'do'], ['wllla', 'wlahi', 'aasma'], ['h', 'i', 'j'], 'h', ['abc', 'def', 'ghi']] 

lst1=[['hey','jude' ,'fox'],['how','you','do'],["wow"],["wllla","wlahi","aasma"],["h","i","j"],["abc","def"]] 
lst2=[['hey','paul'],['how','do'],["wllla"],["h"],["abc","def","ghi"]] 

出力を入れ子にする機能を に減らします。