Python:找出两条格言的区别

Python:找出两条格言的区别,python,dictionary,mapping,Python,Dictionary,Mapping,我有以下问题:给定两个dict,其中article id作为键,title+author作为值。我想使用文章ID来比较这两个dict:如果文章ID有不同的标题/作者,我想创建一个映射,由一个字符串组成,该字符串首先输出旧的文章ID、标题和作者,以及带有相应标题和作者的新ID 例如: old = {u'2014_en_1': u'Letter A\tauthor A\n', u'2014_en_2': u'Explanation\tauthor B\n', u'2014_en_3': u'Conc

我有以下问题:给定两个dict,其中article id作为键,title+author作为值。我想使用文章ID来比较这两个dict:如果文章ID有不同的标题/作者,我想创建一个映射,由一个字符串组成,该字符串首先输出旧的文章ID、标题和作者,以及带有相应标题和作者的新ID

例如:

old = {u'2014_en_1': u'Letter A\tauthor A\n', u'2014_en_2': u'Explanation\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': u'Conclusion\tauthor C\n', u'2014_en_4': u'Explanation\tauthor B\n',}

for k, v in old.iteritems():
    if old[k] != new[k]:
        print k + "\t" + old[k] + # HOW can I find the corresponding article in new?
因此,所需的输出应为:

[]    []    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
我该怎么做?这很棘手,因为新的dict可能会有新的文章(反之亦然):/
谢谢你的帮助

如果您反转
旧的
映射,使值(标题、作者)变为键,会更容易

# Get all keys   
keys = set(old.keys()).union(set(new.keys()))

# Reverse the new dict
new_reverse =  {v:k for k,v in new.items()}

# Loop keys and output
for k in keys:
    if k in old:
        if old[k] != new[k]:
            v = old[k]
            k_in_new = new_reverse[v]
            v_in_new = new[k_in_new]
        else:
            k_in_new = k
            v_in_new = v

        print '%s %s %s %s' % (k, old[k], k_in_new, v_in_new)
    else:
        print '[] [] %s %s' % (k, new[k])
然后您可以迭代
new
并尝试匹配ID:

old_reverse = {v: k for k, v in old.items()}
for k, v in new.iteritems():
    try:
        old_k = old_reverse[v]
        print "%s\t%s\t%s\t%s" % (old_k, repr(v), k, repr(v),)
    except KeyError:
        print "[]\t[]\t%s\t%s" % (k, repr(v),)
请注意,我使用了
repr
使输出更具可读性。相反,您可能希望应用一些自己的字符串操作来获得所需的输出格式

词典是Python中未排序的集合。如果要对它们进行排序,可以使用额外的步骤,将输出存储在元组列表中,然后按顺序打印:

# Flip the dict
old_reverse = {v: k for k, v in old.items()}

# Map new VS old
data = []
for k, v in new.iteritems():
    try:
        old_k = old_reverse[v]
        data.append((old_k, v, k, v,))
    except KeyError:
        data.append((None, None, k, v,))

# Print them sorted
for old_k, old_v, k, v in sorted(data, key=lambda d: d[0]):
    print "%s\t%s\t%s\t%s" % (
        old_k if old_k is not None else "[]",
        repr(old_v) if old_k is not None else "[]",
        k, 
        repr(v),
    ) 

非常感谢您的建议!:)这正是我想做的!而且……哇,只有7行!:O我想问的是:是否有可能对输出进行排序,可能是s.t.首先打印旧目录中没有对应关系的新文章,然后打印新目录中属于第X条的旧目录中的art_1?@MarkF6:有可能,但这涉及到一个额外步骤,即将数据存储在列表中并按顺序打印。看看更新的答案。哇,这真是太棒了!:)非常感谢!:)