小命令的简单解决方案是
dict1 = {"a":0.6, "b":0.3, "c":0.9, "d":1.2, "e":0.2}dict2 = {"a":1.4, "b":7.7, "c":9.0, "d":2.5, "e":2.0}k1 = sorted(dict1, key=dict1.get)k2 = sorted(dict2, key=dict2.get)diffs = dict((k, k2.index(k) - k1.index(k)) for k in dict1)
适用于较大字典的更有效,更易理解的版本:
ranks1 = dict(map(reversed, enumerate(sorted(dict1, key=dict1.get))))ranks2 = dict(map(reversed, enumerate(sorted(dict2, key=dict2.get))))diffs = dict((k, ranks2[k] - ranks1[k]) for k in dict1)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)