比较来自不同语法python的2个列表

时间:2017-12-12 14:53:33

标签: python list comparison

我对Python很陌生,我想比较每个列表中的每个元素:

a=[]
b=[]


with open('dump.sql', 'r') as dump:
    for line in dump:
        if string1 in line:
                tail1 = line.split(string1)[1]
                for group1 in tail1.split("),("):
                        group1 = group1.replace("\'", "").replace("(", "").replace(")\n", "")
                        a.append(group1.split(",")[3].strip())
        if string2 in line:
                tail2 = line.split(string2)[1]
                for group2 in tail2.split("),("):
                        group2 = group2.replace("\'", "").replace("(","").replace(")\n","")
                        b.append(group2.split(",")[3].strip())

由于这2个列表中有2个单独的“if”,可以比较吗?如果是,怎么样? 我想知道,因为如果我试图在第二个列表中打印a,如果我没有得到任何东西,那么我认为应该做的不同于在第二个中附加以下部分:

if(set(naptr)&set(naptra)):
       c.append(group2.split(",")[4].strip())

祝你好运, 丹

1 个答案:

答案 0 :(得分:0)

也许你可以查看这个Python模块:dfflib

这是一个例子:

list1 = [1, 2, 3]
list2 = [1, 3, 4, 5]
diffGenerator = difflib.ndiff(list1, list2)
for diff in diffGenerator:
    print(diff)

结果将是:

  1
- 2
  3
+ 4
+ 5