Skip to content
Advertisement

Python – compare two string by words using difflib and print only difference

Python newbie here. I have the following code to compare two strings using difflab library. The output is prefixed with ‘+’,’-‘ for words which are different. How to get only the differences printed without any prefix?

The expected output for the below code is

Not in first string: Nvdia

Not in first string: IBM

Not in second string: Microsoft

Not in second string: Google

Not in second string: Oracle

or just Nvdia, IBM, Microsoft, Google, Oracle

JavaScript

Thanks!

Advertisement

Answer

If you don’t have to use difflib, you could use a set and string splitting!

JavaScript

You can also get the shared members with the .intersection()

JavaScript

The Wikipedia has a good section on basic set operations with accompanying Venn diagrams
https://en.wikipedia.org/wiki/Set_(mathematics)#Basic_operations


However, if you have to use difflib (some strange environment or assignment) you can also just find every member with a +- prefix and slice off the all the prefixes

JavaScript

All of these operations result in an iterable of strings, so you can .join() ’em together or similar to get a single result as you do in your Question

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement