2016-06-09 82 views
-1

我这种格式具有数据:从一种格式转换到另一个数据

{'a':['b','c','d'],'bla':['djjd','cop']} 

我想上面的转换成这种格式:

('a',('b','c','d')),('bla',('djjd','cop')) 

什么可以在Python,可以达到以上?

回答

0

您可以使用简单的list comprehension,轮流从字典中的键值对以元组:

>>> d = {'a':['b','c','d'],'bla':['djjd','cop']} 
>>> [(k, tuple(v)) for k, v in d.items()] 
[('a', ('b', 'c', 'd')), ('bla', ('djjd', 'cop'))] 
1

假设你的数据在可变d。然后:

converted = [(key, tuple(d[key])) for key in sorted(d)] 
相关问题