Python - getting just the difference between strings

后端 未结 6 2094
心在旅途
心在旅途 2021-01-12 08:12

What\'s the best way of getting just the difference from two multiline strings?

a = \'testing this is working \\n testing this is working 1 \\n\'
b = \'testi         


        
6条回答
  •  心在旅途
    2021-01-12 08:44

    This is basically @Godron629's answer, but since I can't comment, I'm posting it here with a slight modification: changing difference for symmetric_difference so that the order of the sets doesn't matter.

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

提交回复
热议问题