2017-10-07 118 views
-1

列表的两个列表我有两个列表比较蟒蛇

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

我想每个列表中LST1与LST2每个列表进行比较。如果它们的常用单词大于或等于1.那么我需要将更大的列表追加到新列表中。如果他们有共同的词< 1那么我需要将这两个列表追加到同一个新列表。
例如:['hey', 'jude','fox']['hey','paul']进行比较,因为它们具有重复一次的常用单词hey。我需要将更大的列表['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”的意思你是指单词的长度还是单词的匹配?我接受了这个单词。

测试:

与你的列表:

输出:

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

当列表中的一个有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'] 

当第二个列表具有最大公共列表的话它的匹配情况:

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"]] 

输出:

[['hey', 'jude', 'fox'], ['how', 'you', 'do'], ['wllla', 'wlahi', 'aasma'], ['h', 'i', 'j'], 'h', ['abc', 'def', 'ghi']] 
如果你想避免嵌套打破逻辑

出功能 减少嵌套。