我想将字典拆分为两个列表。一个列表用于键,另一个列表用于值。
它应该作为原始订购
原始列表:
[{"car":45845},{"house": 123}]
预期结果:
list1 = ["car", "house"]
list2 = [45845, 123]
答案 0 :(得分:4)
fixed_list = [x.items() for x in list]
keys,values = zip(*fixed_list)
答案 1 :(得分:4)
list1 = [k for item in [{"car":45845},{"house": 123}] for k,v in item.iteritems()]
list2 = [v for item in [{"car":45845},{"house": 123}] for k,v in item.iteritems()]
For Python 3 use dict.items()
代替dict.iteritems()
答案 2 :(得分:0)
a =[{"car":45845},{"house": 123}]
list1 = [i.values()[0] for i in a] #iterate over values
list2= [i.keys()[0] for i in a] #iterate over keys
答案 3 :(得分:0)
original = [{"car":45845},{"house": 123}]
a_dict = {}
for o in original:
a_dict.update(o)
print a_dict
print a_dict.keys()
print a_dict.values()
输出:
{'car': 45845, 'house': 123}
['car', 'house']
[45845, 123]