合并两个词典并节省订单

时间:2014-09-29 09:04:12

标签: python dictionary data-structures

我有两本词典:

a = {u'Anthracite': [u'3/optimized/8593793_fpx.tif'],
 u'Black': [u'6/optimized/8593796_fpx.tif'],
 u'Cobalt': [u'9/optimized/8593799_fpx.tif'],
 u'Fire': [u'2/optimized/8593802_fpx.tif'],
 u'Fuschia': [u'5/optimized/8593805_fpx.tif'],
 u'Iris': [u'8/optimized/8593808_fpx.tif'],
 u'Midnight': [u'1/optimized/8593811_fpx.tif']}

b = {u'Anthracite': [u'5/optimized/8593795_fpx.tif'],
 u'Black': [u'8/optimized/8593798_fpx.tif'],
 u'Cobalt': [u'1/optimized/8593801_fpx.tif'],
 u'Fire': [u'4/optimized/8593804_fpx.tif'],
 u'Fuschia': [u'7/optimized/8593807_fpx.tif'],
 u'Iris': [u'0/optimized/8593810_fpx.tif'],
 u'Midnight': [u'3/optimized/8593813_fpx.tif']}

我需要制作这样的词:

c = {u'Anthracite': [u'3/optimized/8593793_fpx.tif', u'5/optimized/8593795_fpx.tif'],
 u'Black': [u'6/optimized/8593796_fpx.tif', u'8/optimized/8593798_fpx.tif'],
....
}

所以我需要从具有相同键的列表中收集所有项目,但我需要保存第一个订单。

词典总是有相同的键

我试着用拉链做这个但是我弄得一团糟

3 个答案:

答案 0 :(得分:0)

为什么不迭代字典并将它们复制到新字典?为简单起见,在以下代码中使用defaultdict:

from collections import defaultdict
c = defaultdict(list)
a = {"foo": ["bar"]}
b = {"foo": ["baz"], "bah": ["foo"]}
for k, v in a.items() + b.items():
    c[k].extend(v)

如果密钥相同,您可以复制第一个字典并更新其内容:

d = a.copy()

for k, v in b.iteritems():
    d[k].extend(v)

请注意,后者会创建一个浅表副本,因此在此过程中也会修改字典a

答案 1 :(得分:0)

如果您想要按字母顺序排列,请使用OrderedDictsort键:

from collections import OrderedDict
srt_keys = sorted(a.keys())

d = OrderedDict()
for k in srt_keys:
    d[k] = a[k] 
    d[k] += b[k]
print d

OrderedDict([(u'Anthracite', [u'3/optimized/8593793_fpx.tif', u'5/optimized/8593795_fpx.tif']), (u'Black', [u'6/optimized/8593796_fpx.tif', u'8/optimized/8593798_fpx.tif']), (u'Cobalt', [u'9/optimized/8593799_fpx.tif', u'1/optimized/8593801_fpx.tif']), (u'Fire', [u'2/optimized/8593802_fpx.tif', u'4/optimized/8593804_fpx.tif']), (u'Fuschia', [u'5/optimized/8593805_fpx.tif', u'7/optimized/8593807_fpx.tif']), (u'Iris', [u'8/optimized/8593808_fpx.tif', u'0/optimized/8593810_fpx.tif']), (u'Midnight', [u'1/optimized/8593811_fpx.tif', u'3/optimized/8593813_fpx.tif'])])

答案 2 :(得分:0)

如何将OrderedDict与元组列表一起使用来设置初始顺序。然后简单地维护它。

请在此处查看我的答案,以获得更好的dict语法:Override the {...} notation so i get an OrderedDict() instead of a dict()?

from collections import OrderedDict

#Use an ordered dict, with a tuple list init to maintain initial order 
a = OrderedDict([
      (u'Anthracite', [u'3/optimized/8593793_fpx.tif']),
      (u'Black', [u'6/optimized/8593796_fpx.tif']),
      (u'Cobalt', [u'9/optimized/8593799_fpx.tif']),
      (u'Fire', [u'2/optimized/8593802_fpx.tif']),
      (u'Fuschia', [u'5/optimized/8593805_fpx.tif']),
      (u'Iris', [u'8/optimized/8593808_fpx.tif']),
      (u'Midnight', [u'1/optimized/8593811_fpx.tif'])
      ])

#We don't care about b's order
b = {u'Anthracite': [u'5/optimized/8593795_fpx.tif'],
 u'Black': [u'8/optimized/8593798_fpx.tif'],
 u'Cobalt': [u'1/optimized/8593801_fpx.tif'],
 u'Fire': [u'4/optimized/8593804_fpx.tif'],
 u'Fuschia': [u'7/optimized/8593807_fpx.tif'],
 u'Iris': [u'0/optimized/8593810_fpx.tif'],
 u'Midnight': [u'3/optimized/8593813_fpx.tif']}

merge =  OrderedDict()
#Since b has the same keys as a(we don't need to care for diffrent keys), but we want a's order
for key in a:
    #We insert by order to an OrderedDict so the same order will be maintained
    merge[key] = a[key] + b[key]