I have two files of dip.md
and tp.md
, which copied respectively from Think Python and Dive Into Python 3
By comparing them, I retrieved the shared words by the following procedures:
Read contents from files
def read(filename): with open(filename) as file: content = file.read() return content dip = read('dip.md') tp = read('tp.md') In [49]: dip Out[49]: '- - \n\n You are here: •\n\n # Dive Into Python 3\n\n Dive Into Python 3 covers Python 3 and its differences from Python 2. Compared to [Dive Into Python](http://diveintopython.net/), it’s about 20% revised and 80% new material. The book is now complete, but [feedback is always welcome](http://www.diveintopython3.net/about.html).\n\n ... In [50]: tp Out[50]: 'Think Python: How to Think Like a Computer Scientist\n\nAllen B. Downey\n\n2nd Edition, Version 2.2.23\n\nPreface\nThe strange history of this book\nAcknowledgments\nContributor List\nThe way of the program\nWhat is a program?\nRunning Python\nThe first program\nArithmetic operators\nValues and types\nFormal and natural languages\nDebugging\nGlossary\nExercises\nVariables, expressions and statements\nAssignment statements\nVariable names\nExpressions and statements\nScript mode\nOrder of operations\nString operations\nComments\nDebugging\nGlossary\nExercises\nFunctions\nFunction calls
Clear data using
string.punctuation
def clear_data(cont): for data in cont: if data in punctuation: cont = cont.replace(data, ' ') cont = cont.replace('\n', ' ') cont = cont.lower() return cont dip_cont = clear_data(dip) tp_cont = clear_data(tip)
Retrieve the qualified list
def get_qualified_list(cont): cont_list = cont.split(' ') qualified_list = [i for i in cont_list if i.isalpha()] return qualified_list dip_list = get_qualified_list(dip_cont) tp_list = get_qualified_list(tp_cont)
Get their intersection
In [51]: print(set(dip_list) & set(tp_list)) {'is', 'study', 'Refactoring', 'with', 'and', 'the', 'first', 'Expressions', 'book', 'as', 'comprehensions', 'of', 'Strings', 'program', 'names', 'expressions', 'Case', 'to', 'Files', 'Classes', 'Objects', 'method', 'are', 'Python', 'The', 'in', 'new', 'strings'}
This solution is a bit too much for such a task which can be easily handled on 'MS Word'.
How can such a task be accomplished in an easy way?