2017-09-27 5 views
3

2つの複数行の文字列との違いを得るにはどうすればよいですか?Python - 文字列の違いを取得する

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) 

これが生成します。

testing this is working 2

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 

正確に取得する最良の方法は何ですか?

ここで解決方法はありますか?

+3

'b.split(a)'? –

+0

Damn @Chris_Rands。それについて決して考えなかった!!ニースハック。 –

+0

@Chris_Rands素敵なハックだけど、それはそれを実行するための演奏方法ではない –

答えて

3
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...' 

基本的に各文字列ラインのセット作り、設定された差をとる - A.ではありませんBで、すなわちすべてのものその後、その結果を受け取り、すべてを1つのストリングに結合します。

編集:これは@ShreyasGが言ったというのがconveluded方法です - [X X用ではないyのXの場合]

0

@Chris_Randsコメントビル(あなたの文字列は、マルチラインであり、あなたが行が1に存在するが、他ではないしたい場合)、あなたはあまりにも分割線()操作を使用することができます。

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'] 
2

最も簡単なハックは、split()を使用することにより、@Chrisクレジット。

注:より長い文字列を判別し、それを分割に使用する必要があります。

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 

EDIT値:@ekhumoroから感謝をreplaceを使用して別のハックのために、必要なjoin計算のいずれかを必要とせず。

if len(a)>len(b): 
    res=a.replace(b,'')    #get diff 
else: 
    res=b.replace(a,'')    #get diff 
+1

'b.replace(a、 '')'はよりシンプルで速く、より理にかなっています。 – ekhumoro

+0

ハハ、これは大好き。別の良いハック。このような方法で 'split'や' replace'を使うことは決して考えませんでした。ありがとうございます。 –

0
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()) 
2

これはGodron629の答え@基本的にですが、私はコメントすることはできませんから...、私はわずかな変更を加えてここに掲載しています:を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...' 
関連する問題