我正在编写一个Python脚本,我需要按照JSON对象列表中的键进行分组。
我在python中列出了大量JSON对象,格式如下:
[{' name':xyz, 领土':abc, ' parameter_a':1, ' parameter_b':2, ' parameter_c':3}, ......]
现在我想创建一个标签(比如parameter_d),它应该说出按整数列出的特定区域(Say'abc')在整个json对象列表中的数量。 例如。区域abc在列表中出现3次,因此我希望parameter_d为发生区域abc的不同实例保存值1,2,3。 在此先感谢您的帮助。
答案 0 :(得分:0)
from json import loads, dumps
from collections import defaultdict
json_string = """
[
{"name": "xyz", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
{"name": "qrs", "territory": "def", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
{"name": "tuv", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
{"name": "abc", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3}
]"""
# Step 1: convert from JSON to Python:
python_object = loads(json_string)
# Step 2: Add `parameter_d` to each item in list,
# using defaultdict(int) as a counter:
counts = defaultdict(int)
for item in python_object:
counts[item["territory"]] += 1
item["parameter_d"] = counts[item["territory"]]
# Step 3: convert from Python to JSON
json_string = dumps(python_object, indent=2)
print json_string
答案 1 :(得分:0)
使用Rob的这部分答案进行设置:
from json import loads, dumps
from collectins import defaultdict
json_string = """
[
{"name": "xyz", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
{"name": "qrs", "territory": "def", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
{"name": "tuv", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
{"name": "abc", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3}
]"""
# Step 1: convert from JSON to Python:
python_object = loads(json_string)
您可以创建地区字典,然后更新您的对象:
territories = defaultdict(list)
for i, item in enumerate(python_object):
territories[item['territory']].append(i)
for item in python_object:
item['parameter_d'] = territories[item['territory']]
print(dumps(python_object, indent=2))