将键加入关联值

时间:2014-07-16 05:47:49

标签: python dictionary key

有人可以告诉我如何将词典的键加入各自的价值观吗?例如:

B = {'Mary': [(9, 10), (250, 550), (850, 1000), (1000, 3000)],
     'john': [(5, 36), (20, 100), (500, 1000), (800, 3000)]}

我希望输出为:

(Mary, 9, 10), (Mary,250, 550), (Mary,850, 1000), (Mary,1000, 3000)
(john,5,36), (john,20,100), (john,500,1000), (john,800,3000)

1 个答案:

答案 0 :(得分:2)

In [7]: [(key,) + pair for key, value in B.items() for pair in value]
Out[7]: 
[('Mary', 9, 10),
 ('Mary', 250, 550),
 ('Mary', 850, 1000),
 ('Mary', 1000, 3000),
 ('john', 5, 36),
 ('john', 20, 100),
 ('john', 500, 1000),
 ('john', 800, 3000)]