我是python的新手。刚开始尝试小程序。我在这里看到了这个问题:
json的输入可以是这种格式:
'[ ["a","b","c"], [1,2,null], [null,3,4], [5,null,6] ]'
或:
'[ { "a":1, "b":2 }, { "b":3, "c":4 }, { "c":6, "a":5 } ]'
我们应该将其转换为:
output = '{ "a": [1,null,5], "b": [2,3,null], "c": [null,4,6] }'
到目前为止,我可以想到检查每个元素并追加结果。在python中有没有简单或更好的方法来做到这一点。请赐教。
答案 0 :(得分:2)
以这种方式使用collections module
中的defaultdict
:
>>> import json
>>> s = '[ { "a":1, "b":2 }, { "b":3, "c":4 }, { "c":6, "a":5 } ]'
>>>
>>> dic = json.loads(s)
>>> dic
[{'a': 1, 'b': 2}, {'b': 3, 'c': 4}, {'a': 5, 'c': 6}]
>>> kys = set(k for sub_d in d for k in sub_d) #creates uniques keys of dictionary d
>>> kys
{'a', 'b', 'c'}
>>>
>>> from collections import defaultdict
>>> my_dict = defaultdict(list)
>>> for d in dic:
for k in kys:
my_dict[k].append(d.get(k, None))
>>> my_dict
defaultdict(<class 'list'>, {'a': [1, None, 5], 'b': [2, 3, None], 'c': [None, 4, 6]})
至于其他情况:
>>> s = '[ ["a","b","c"], [1,2,null], [null,3,4], [5,null,6] ]'
>>> d = json.loads(s)
>>> d
[['a', 'b', 'c'], [1, 2, None], [None, 3, 4], [5, None, 6]]
>>> my_dict = dict(zip(d[0], zip(*d[1:])))
>>> my_dict
{'a': (1, None, 5), 'b': (2, 3, None), 'c': (None, 4, 6)}
如果您不想将元组作为值,那么:
>>> my_dict = defaultdict(list)
>>> for k,v in zip(d[0], zip(*d[1:])):
my_dict[k].extend(v)
最后,将两个案例分组为一个函数:
import json
from collections import defaultdict
def parse_data(data):
data = json.loads(data)
my_dict = defaultdict(list)
if isinstance(data[0], list):
for k,v in zip(data[0], zip(*data[1:])):
my_dict[k].extend(v)
elif isinstance(data[0], dict):
kys = set(k for sub_d in data for k in sub_d)
for d in data:
for k in kys:
my_dict[k].append(d.get(k, None))
return my_dict
s1 = '[ ["a","b","c"], [1,2,null], [null,3,4], [5,null,6] ]'
d1 = parse_data(s1)
s2 = '[ { "a":1, "b":2 }, { "b":3, "c":4 }, { "c":6, "a":5 } ]'
d2 = parse_data(s2)
答案 1 :(得分:1)
它适用于两种结构
import json
def convert_format(json_data):
convert_to_py_obj = json.loads(json_data)
new_format = dict()
if isinstance(convert_to_py_obj, list) and len(convert_to_py_obj) > 1:
index_0_keys = convert_to_py_obj[0]
if isinstance(index_0_keys, list):
for i, key in enumerate(index_0_keys):
new_format[key] = []
for sub_list in convert_to_py_obj[1:]:
new_format[key].append(sub_list[i])
elif isinstance(index_0_keys, dict):
for sub_dict in convert_to_py_obj:
for key, val in sub_dict.iteritems():
if key in new_format:
new_format[key].append(val)
else:
new_format[key] = [val]
none_keys = set(new_format.keys()) - set(sub_dict.keys())
for key in none_keys:
new_format[key].append(None)
return json.dumps(new_format)
答案 2 :(得分:1)
使用Python 3
:
def get_elements(json_txt):
import json
arr = json.loads(json_txt)
new = {}
list_of_keys = []
list_of_keys_from_dicts = [list(elem.keys()) for elem in arr]
# getting keys from json
for keys in list_of_keys_from_dicts:
for key in keys:
if key not in list_of_keys:
list_of_keys.append(key)
for key in list_of_keys:
new[key] = []
for element in arr:
for key in list_of_keys:
if key in element:
new[key].append(element[key])
else:
new[key].append(None)
return json.dumps(new)