如何将列表中的项目改为json

时间:2017-12-06 12:32:32

标签: python json

我有4个分离的json,其中有4个键summinmaxcount,列表如下:

"sum" :  ["columnA", "columnB", "columnC", "columnD"]

"min" :  ["columnA", "columnC"]

"max" :  ["columnB", "columnC"]

"count" : ["columnA", "columnD"]

我如何将它们重新打包成这样的json对象:

{ "columnA" : ["sum", "min","count" ] , "columnB" : ["sum", "max"], "columnC" : ["sum", "min", "max"] }

1 个答案:

答案 0 :(得分:1)

对于快速(开发)结果,请尝试:

>>> result = {}
>>> for function, columns in my_json.items():
        for column in columns:
            result.setdefault(column, []).append(function)


>>> result
{'columnD': ['count', 'sum'], 'columnA': ['count', 'sum', 'min'], 'columnC': ['max', 'sum', 'min'], 'columnB': ['max', 'sum']}

更易阅读的版本:

SegmentEnd opposite(SegmentEnd e)
{
    return e == &Segment::_p1 ? &Segment::_p2 : &Segment::_p1;
}

// ...
Point& one(segment.*end);
Point& other(segment.*opposite(end));