dictionary - Python: find differences in two dicts -
I have the following problem: Two documents with articles are given as key and title + author value. I would like to compare two dates using article-id: If the article ID has different titles / authors, then I would like to create a mapping, in which a string will be found which is the old article id and title and author's output New ID with and does the same title and author
Example:
Older = {u'2014_en_1 '}: u'Letter A \ tauthor A \ n', u'2014_en_2 ': u'xplanation \ tauthor B \ N ', u'2014_en_3': u'Conclusion \ tauthor c \ n '} new = {u'2014_en_1': u'Welcome \ tauthor xy \ n ', u'2014_en_2': u'Letter A \ tauthor A \ N ', U'2014_en_3': for 'ending \ tauthor C \ n', u'2014_en_4 ': u'xplanation \ tauthor B \ n',} for k, in old.iteritems (v): if the old [k] ]! = New [k]: print k + "\ t" + old [k] + # How can I get articles related to the new one? So, the desired output should be:
[] [] 2014_en_1 Welcome \ tauthor XY 2014_en_1 letter A \ tauthor A 2014_en_2 Letter A \ tauthor A 2014_en_2 Explanation \ tauthor B-2014_en_4 Explanation \ tauthor B-2014_en_3 Conclusion \ tauthor C 2014_en_3 Conclusion \ tauthor C How can I do this? This is difficult because it is possible that the new dictionary can contain new articles (or vice versa): / Thanks for any help!
If you change your old mapping, then it's easy to set the value ( For title v, old.items ()} for title, old_reverse = {v: k}
, In new.iteritems () v: try: old_k = old_reverse [v] print "% s \ t% s \ t% s \ t% s"% (old_k), keyrror: print "[] \ t [] \ In addition to t% s \ t% s "% (k, repr (v),), repr (v), k, repr (v),)
Note that I < Code> repr is used to make output slightly more readable. Instead, you may want to apply some of your own string manipulation to get the desired output format.
The dictionaries contain a stacked collection in Python. If you want to sort them, you can use an extra step by storing the output in the list of Tulips and then sorting it and printing:
flip to # dict Do old_reverse = {v: kK, old.items (#) in v # new VS For older data = [], v. in new.iteritems (): try: old_k = old_reverse [v] data.append ((old_k, v, k, v),) except for keyError: data.append (( No, none, K, V,)) Sort them for sorted old_k, old_av, k, sorted (data, key = lambda d: d [0]): print "% s \ (Old_v) If old_k is not another "[]", k, repr (v),) Code>
Comments
Post a Comment