首页 > 解决方案 > 如何比较嵌套列表并将不匹配项追加到python3中的新列表?

问题描述

I want to compare 2 nested list. compare index 1 of list 1 with list 2 index 1 and appedn list 1 , list 2 to the new list.  in python3
            l1 = [['a','bat','c'], ['a','doll','c']]
            l2 = [['a','flowers','c'],['a','god','c'],['a','bat','c']]              
            output = []
            for i in l1:
                for j in l2:
                        if i[1] != j[1]:
                            output.append(j)
                            
                break           
            print(output)           
            Expected Output:
            [['a','doll','c'] ,['a','flowers','c'],['a','god','c']
            

我想比较 2 个嵌套列表。比较列表 1 的索引 1 与列表 2 的索引 1 并将列表 1 和列表 2 添加到新列表。在python3中

标签: python-3.xfor-loopcomparecomparisonnested-lists

解决方案


推荐阅读