代码之家  ›  专栏  ›  技术社区  ›  Rekovni

Python-仅获取字符串之间的差异

  •  9
  • Rekovni  · 技术社区  · 8 年前

    a = 'testing this is working \n testing this is working 1 \n'
    b = 'testing this is working \n testing this is working 1 \n testing this is working 2'
    
    diff = difflib.ndiff(a,b)
    print ''.join(diff)
    

    这将产生:

      t  e  s  t  i  n  g     t  h  i  s     i  s     w  o  r  k  i  n  g     
         t  e  s  t  i  n  g     t  h  i  s     i  s     w  o  r  k  i  n  g     1     
    +  + t+ e+ s+ t+ i+ n+ g+  + t+ h+ i+ s+  + i+ s+  + w+ o+ r+ k+ i+ n+ g+  + 2
    

    testing this is working 2 ?

    正则表达式是这里的解决方案吗?

    6 回复  |  直到 8 年前
        1
  •  7
  •   Kaushik NP    8 年前

    最简单的黑客,信用卡 @Chris ,通过使用 split() .

    注: 您需要确定哪个字符串较长,并将其用于拆分。

    if len(a)>len(b): 
       res=''.join(a.split(b))             #get diff
    else: 
       res=''.join(b.split(a))             #get diff
    
    print(res.strip())                     #remove whitespace on either sides
    

    IN : a = 'testing this is working \n testing this is working 1 \n' 
    IN : b = 'testing this is working \n testing this is working 1 \n testing this is working 2'
    
    OUT : testing this is working 2
    

    编辑: 幸亏 @ekhumoro replace ,无需任何 join 需要计算。

    if len(a)>len(b): 
        res=a.replace(b,'')             #get diff
    else: 
        res=b.replace(a,'')             #get diff
    
        2
  •  6
  •   Godron629    8 年前
    a = 'testing this is working \n testing this is working 1 \n'
    b = 'testing this is working \n testing this is working 1 \n testing this is working 2'
    
    splitA = set(a.split("\n"))
    splitB = set(b.split("\n"))
    
    diff = splitB.difference(splitA)
    diff = ", ".join(diff)  # ' testing this is working 2, more things if there were...'
    

    本质上,把每个字符串都变成一组行,取集合差,即B中不在a中的所有东西,然后取结果,将其全部合并到一个字符串中。

        3
  •  5
  •   Keith    8 年前

    这基本上是@Godron629的答案,但由于我无法发表评论,我在这里发布了一个小小的修改:改变 difference 对于 symmetric_difference 所以集合的顺序无关紧要。

    a = 'testing this is working \n testing this is working 1 \n'
    b = 'testing this is working \n testing this is working 1 \n testing this is working 2'
    
    splitA = set(a.split("\n"))
    splitB = set(b.split("\n"))
    
    diff = splitB.symmetric_difference(splitA)
    diff = ", ".join(diff)  # ' testing this is working 2, some more things...'
    
        4
  •  2
  •   pylang    8 年前
    import itertools as it
    
    
    "".join(y for x, y in it.zip_longest(a, b) if x != y)
    # ' testing this is working 2'
    

    import collections as ct
    
    
    ca = ct.Counter(a.split("\n"))
    cb = ct.Counter(b.split("\n"))
    
    diff = cb - ca
    "".join(diff.keys())
    
        5
  •  0
  •   ShreyasG    8 年前

    b_s = b.splitlines()
    a_s = a.splitlines()
    [x for x in b_s if x not in a_s]
    

    预期输出为:

    [' testing this is working 2']
    
        6
  •  0
  •   Michael Scott Asato Cuthbert    4 年前

    def helper(a, b):
    
        for i, l_a in enumerate(a):
            if b == l_a:
                return i
        return -1
    
    def diff(a, b):
    
        t_b = b
        c_i = 0
        for c in a:
    
            t_i = helper(t_b, c)
            if t_i != -1 and (t_i > c_i or t_i == c_i):
                c_i = t_i
                t_b = t_b[:c_i] + t_b[c_i+1:]
    
        t_a = a
        c_i = 0
        for c in b:
    
            t_i = helper(t_a, c)
            if t_i != -1 and (t_i > c_i or t_i == c_i):
                c_i = t_i
                t_a = t_a[:c_i] + t_a[c_i+1:]
    
        return t_b + t_a
    

    使用示例 打印差异(a,b)