Python使用多个键对JSON对象进行分组
Python grouping JSON object using multiple keys
我有这个JSON对象,其结构如下(JSON对象是使用to_json(orient="records")
从pandas数据框中提取的)
data = [{'month': 'Jan','date': '18','activity': 'cycling','duration': 3},
{'month': 'Jan', 'date': '18','activity': 'reading', 'duration': 3.0},
{'month': 'Jan', 'date': '19', 'activity': 'scripting', 'duration': 19.5},
{'month': 'Feb','date': '18', 'activity': 'work', 'duration': 22.0 },
{'month': 'Feb', 'date': '19', 'activity': 'cooking','duration': 0.7},
{'month': 'March', 'date': '16', 'activity': 'hiking', 'duration': 8.0}]
我试图通过两个字段month
和date
分组预期结果:
data = [{
"month": "Jan",
"details": [{
"date": "18",
"effort": [{
"activity": "cycling",
"duration": 3
}, {
"activity": "reading",
"duration": 3.0
}]
}, {
"date": "19",
"effort": [{
"activity": "scripting",
"duration": 19.5
}]
}]
}, {
"month": "Feb",
"details": [{
"date": "18",
"effort": [{
"activity": "work",
"duration": 22.0
}]
}, {
"date": "19",
"effort": [{
"activity": "cooking",
"duration": 0.7
}]
}]
}, {
"month": "March",
"details": [{
"date": "16",
"effort": [{
"activity": "hiking",
"duration": 8.0
}]
}]
}]
我尝试使用to_dict(orient="records")
从pandas数据框中提取的数据作为python字典
list_ = []
for item in dict_:
list_.append({"month" : item["month"],
"details":
[{
"date" : item["date"],
"efforts" :
[{
"activity" : item["activity"],
"duration": item["duration"]
}]
}]
})
json.dumps(list_)
我得到的输出是
[{
"month": "Jan",
"details": [{
"date": "18",
"efforts": [{
"duration": 3,
"activity": "cycling"
}]
}]
}, {
"month": "Jan",
"details": [{
"date": "18",
"efforts": [{
"duration": 3.0,
"activity": "reading"
}]
}]
}, {
"month": "Jan",
"details": [{
"date": "19",
"efforts": [{
"duration": 19.5,
"activity": "scripting"
}]
}]
}, {
"month": "Feb",
"details": [{
"date": "18",
"efforts": [{
"duration": 22.0,
"activity": "work"
}]
}]
}, {
"month": "Feb",
"details": [{
"date": "19",
"efforts": [{
"duration": 0.7,
"activity": "cooking"
}]
}]
}, {
"month": "March",
"details": [{
"date": "16",
"efforts": [{
"duration": 8.0,
"activity": "hiking"
}]
}]
}]
我不处理值与现有字段的连接。
尝试使用python以及java-script,你们有什么建议或解决问题的方法吗?由于
这似乎可以工作:
<标题> 代码data = [{'month': 'Jan','date': '18','activity': 'cycling','duration': 3},
{'month': 'Jan', 'date': '18','activity': 'reading', 'duration': 3.0},
{'month': 'Jan', 'date': '19', 'activity': 'scripting', 'duration': 19.5},
{'month': 'Feb','date': '18', 'activity': 'work', 'duration': 22.0 },
{'month': 'Feb', 'date': '19', 'activity': 'cooking','duration': 0.7},
{'month': 'March', 'date': '16', 'activity': 'hiking', 'duration': 8.0}]
new_data = []
not_found = True
for item in data:
for month in new_data:
not_found = True
if item['month'] == month['month']:
not_found = False
for date in month['details']:
if item['date'] == date['date']:
date['effort'].append({'activity':item['activity'], 'duration':item['duration']})
else:
month['details'].append({'date':item['date'], 'effort':[{'activity':item['activity'], 'duration':item['duration']}]})
break
if not_found:
new_data.append({'month':item['month'], 'details':[{'date':item['date'], '
'effort':[{'activity':item['activity'], 'duration':item['duration']}]}]})
print new_data
<标题> 输出[{'details': [{'date': '18', 'effort': [{'duration': 3, 'activity': 'cycling'}, {'duration': 3.0, 'activity': 'reading'}]}, {'date': '19', 'effort': [{'duration': 19.5, 'activity': 'scripting'}, {'duration': 19.5, 'activity': 'scripting'}]}], 'month': 'Jan'}, {'details': [{'date': '18', 'effort': [{'duration': 22.0, 'activity': 'work'}]}, {'date': '19', 'effort': [{'duration': 0.7, 'activity': 'cooking'}, {'duration': 0.7, 'activity': 'cooking'}]}], 'month': 'Feb'}, {'details': [{'date': '16', 'effort': [{'duration': 8.0, 'activity': 'hiking'}]}], 'month': 'March'}]
基本上只是遍历每个条目,首先检查月份是否存在,如果存在,检查日期是否已经存在,并相应地附加到新数据上。如果不存在月份,就附加所有内容,如果不存在日期,就附加日期细节和新活动。如果日期也存在,那么只需添加活动
标题>标题>对JSON进行分组的通用函数。您必须传递字段到组和组的键数组名称
def groupBy(vetor, campos, pos):
if(pos >= len(campos)):
return vetor
gmx = campos[pos]
agrupado = gmx["field"]
kx = gmx["gbkey"]
tam = len(campos)
agrupados = {}
saida = {}
retorno = []
for l in vetor:
lmf = {}
for k, s in l.items():
val_agrupado = l[agrupado]
if not (val_agrupado in agrupados):
agrupados[val_agrupado] = []
if agrupado != k:
lmf[k] = s
agrupados[val_agrupado].append(lmf)
for l in agrupados:
agrup = agrupados[l]
if(len(campos) > 1):
agrup = groupBy(agrup, campos, pos + 1)
saida = {}
saida[agrupado] = l
saida[kx] = agrup
retorno.append(saida)
return retorno
data = [{'month': 'Jan','date': '18','activity': 'cycling','duration': 3},
{'month': 'Jan', 'date': '18','activity': 'reading', 'duration': 3.0},
{'month': 'Jan', 'date': '19', 'activity': 'scripting', 'duration': 19.5},
{'month': 'Feb','date': '18', 'activity': 'work', 'duration': 22.0 },
{'month': 'Feb', 'date': '19', 'activity': 'cooking','duration': 0.7},
{'month': 'March', 'date': '16', 'activity': 'hiking', 'duration': 8.0}]
print(groupBy(data, [{'field':'month', 'gbkey': 'details'}, {'field':'date', 'gbkey': 'effort'}], 0))
它会生成类似
的内容[
{
"month":"Jan",
"details":[
{
"date":"18",
"effort":[
{
"activity":"cycling",
"duration":3
},
{
"activity":"reading",
"duration":3.0
}
]
},
{
"date":"19",
"effort":[
{
"activity":"scripting",
"duration":19.5
}
]
}
]
},
{
"month":"Feb",
"details":[
{
"date":"18",
"effort":[
{
"activity":"work",
"duration":22.0
}
]
},
{
"date":"19",
"effort":[
{
"activity":"cooking",
"duration":0.7
}
]
}
]
},
{
"month":"March",
"details":[
{
"date":"16",
"effort":[
{
"activity":"hiking",
"duration":8.0
}
]
}
]
}
]
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 我应该如何将响应数据保存在对象(json)中以获得更好的操作和性能
- 用javascript从列表对象(JSON的)构建diffrent选项卡
- php请求带有多个对象json-jquery
- AngularJS,过滤器:如何将一个巨大的对象(JSON)变成一个数组
- 动态属性对象 - JSON
- JavaScript 对象 (JSON) 中的动态设置值
- 从数组查询获取对象 JSON 值时出错
- 使用jQuerygetJSON将多维对象JSON转换为HTML
- 需要从 Angular JS 应用程序中的另一个 JSON 对象数组填充 JSON 数组的每个对象 JSON 对象数组
- 嵌套对象JSON排序JavaScript
- PHP在MySQL中保存来自Javascript的对象"JSON.stringify"通过Ajax
- 将JavaScript对象/ JSON转换为PHP数组
- JSON对象.JSON对象内部
- 访问对象的对象- JSON API
- 数组到对象JSON
- Message":"传入的无效对象(JSON数据格式化问题)