结合基于值的列表字典

时间:2019-06-04 17:06:41

标签: python list dictionary

我有一个字典列表,如果ID相同,我想实现的目的是合并字典中的某些元素。

例如,可以在此处找到数据集的摘要:

[... , {'time': 1554283273.0824468, 'quaternion': [0.17855453309035293, 0.005453680566358193, -0.9834562739434834, -0.0300381977216707], 'id': 13762}
{'time': 1554283273.1220775, 'quaternion': [-0.9705062538096711, -0.029642658768302424, -0.23913505674039495, -0.007304021853201095], 'id': 13762}
{'time': 1554283273.1620576, 'quaternion': [0.836337807006856, 0.02554468467443435, -0.5473637151080412, -0.016718404199258217], 'id': 13762}, ...]

因此,在这种情况下,我想要实现的是一个字典和按时间顺序排列的结果元组,因此它看起来如下所示:

[..., {'time':(1554283273.0824468, 1554283273.1220775, 1554283273.1620576), 'quaternion': ([0.17855453309035293, 0.005453680566358193, -0.9834562739434834, -0.0300381977216707], [-0.9705062538096711, -0.029642658768302424, -0.23913505674039495, -0.007304021853201095], [0.836337807006856, 0.02554468467443435, -0.5473637151080412, -0.016718404199258217]), 'id': 13762}, ...]

5 个答案:

答案 0 :(得分:0)

使用collections.defaultdict

from collections import defaultdict

lst = [{'time': 1554283273.0824468, 'quaternion': [0.17855453309035293, 0.005453680566358193, -0.9834562739434834, -0.0300381977216707], 'id': 13762},
       {'time': 1554283273.1220775, 'quaternion': [-0.9705062538096711, -0.029642658768302424, -0.23913505674039495, -0.007304021853201095], 'id': 13762},
       {'time': 1554283273.1620576, 'quaternion': [0.836337807006856, 0.02554468467443435, -0.5473637151080412, -0.016718404199258217], 'id': 13762}]

res = []
for x in lst:
    d = defaultdict(tuple)
    d['time'] += (x['time'],)
    d['quaternion'] += (x['quaternion'],)
    d['id'] = x['id']
    res.append(d)

print(res)
# [{'time':(1554283273.0824468, 1554283273.1220775, 1554283273.1620576), 
#   'quaternion': ([0.17855453309035293, 0.005453680566358193, -0.9834562739434834, -0.0300381977216707], [-0.9705062538096711, -0.029642658768302424, -0.23913505674039495, -0.007304021853201095], [0.836337807006856, 0.02554468467443435, -0.5473637151080412, -0.016718404199258217]),
#   'id': 13762}]

答案 1 :(得分:0)

您在这里:

l = [{'time': 1554283273.0824468, 'quaternion': [0.17855453309035293, 0.005453680566358193, -0.9834562739434834, -0.0300381977216707], 'id': 13762},
{'time': 1554283273.1220775, 'quaternion': [-0.9705062538096711, -0.029642658768302424, -0.23913505674039495, -0.007304021853201095], 'id': 13762},
{'time': 1554283273.1620576, 'quaternion': [0.836337807006856, 0.02554468467443435, -0.5473637151080412, -0.016718404199258217], 'id': 13762}]

all_id = set(dict_['id'] for dict_ in l) # All unique 'id' in the list
new_l = []
for id_ in all_id:
    all_time = [dict_['time'] for dict_ in l if dict_['id'] == id_]
    all_q = [dict_['quaternion'] for dict_ in l if dict_['id'] == id_]
    new_l.append({'time' : all_time, 'quaternion' : all_q, 'id' : id_})

输出

# new_l becomes :
[{'quaternion': [[0.17855453309035293, 0.005453680566358193, -0.9834562739434834, -0.0300381977216707], [-0.9705062538096711, -0.029642658768302424, -0.23913505674039495, -0.007304021853201095], [0.836337807006856, 0.02554468467443435, -0.5473637151080412, -0.016718404199258217]], 'id': 13762, 'time': [1554283273.0824468, 1554283273.1220775, 1554283273.1620576]}]

答案 2 :(得分:0)

您可以使用默认字典来执行此操作,以为每个“ id”累积较小的字典。要获取字典列表作为输出,可以从默认字典中获取.values():

dicts =[ {'time': 1554283273.0824468, 'quaternion': [0.17855453309035293, 0.005453680566358193, -0.9834562739434834, -0.0300381977216707], 'id': 13762},
         {'time': 1554283273.1220775, 'quaternion': [-0.9705062538096711, -0.029642658768302424, -0.23913505674039495, -0.007304021853201095], 'id': 13762},
         {'time': 1554283273.1620576, 'quaternion': [0.836337807006856, 0.02554468467443435, -0.5473637151080412, -0.016718404199258217], 'id': 13762}
       ]

from collections import defaultdict
combined = defaultdict(dict)
for d in dicts:
    combined[d["id"]]["id"] = d["id"]
    combined[d["id"]].setdefault("time",[]).append(d["time"])
    combined[d["id"]].setdefault("quaternion",[]).extend(d["quaternion"])
combined = list(combined.values())
print(combined)

# [{'id': 13762, 'time': [1554283273.0824468, 1554283273.1220775, 1554283273.1620576], 'quaternion': [0.17855453309035293, 0.005453680566358193, -0.9834562739434834, -0.0300381977216707, -0.9705062538096711, -0.029642658768302424, -0.23913505674039495, -0.007304021853201095, 0.836337807006856, 0.02554468467443435, -0.5473637151080412, -0.016718404199258217]}]

答案 3 :(得分:0)

这是通用解决方案。 功能:

def group_dict_by_key(list_of_dicts, key):
    res = {}
    for item in list_of_dicts:
        if item[key] not in res:
            res[item[key]] = {key: item[key]}
        temp = res[item[key]]
        for k, v in item.items():
            if k == key:
                continue
            elif k in temp:
                temp[k] += (v,)
            else:
                temp[k] = (v,)
    return list(res.values())

输入数据:

d = [{'time': 1554283273.0824468, 'quaternion': [0.17855453309035293, 0.005453680566358193, -0.9834562739434834, -0.0300381977216707], 'id': 13762},
{'time': 1554283273.1220775, 'quaternion': [-0.9705062538096711, -0.029642658768302424, -0.23913505674039495, -0.007304021853201095], 'id': 13762},
{'time': 1554283273.1620576, 'quaternion': [0.836337807006856, 0.02554468467443435, -0.5473637151080412, -0.016718404199258217], 'id': 13762},
{'time': 1554283273.0824468, 'quaternion': [0.17855453309035293, 0.005453680566358193, -0.9834562739434834, -0.0300381977216707], 'id': 13763},
{'time': 1554283273.1220775, 'quaternion': [-0.9705062538096711, -0.029642658768302424, -0.23913505674039495, -0.007304021853201095], 'id': 13763},
{'time': 1554283273.1620576, 'quaternion': [0.836337807006856, 0.02554468467443435, -0.5473637151080412, -0.016718404199258217], 'id': 13763}]

用法:

final = group_dict_by_key(d, 'id')

输出:

[
    {
        'id': 13762,
        'time': (1554283273.0824468, 1554283273.1220775, 1554283273.1620576),
        'quaternion': (
            [0.17855453309035293, 0.005453680566358193, -0.9834562739434834, -0.0300381977216707],
            [-0.9705062538096711, -0.029642658768302424, -0.23913505674039495, -0.007304021853201095],
            [0.836337807006856, 0.02554468467443435, -0.5473637151080412, -0.016718404199258217]
        )
    }, {
        'id': 13763,
        'time': (1554283273.0824468, 1554283273.1220775, 1554283273.1620576),
        'quaternion': (
            [0.17855453309035293, 0.005453680566358193, -0.9834562739434834, -0.0300381977216707],
            [-0.9705062538096711, -0.029642658768302424, -0.23913505674039495, -0.007304021853201095],
            [0.836337807006856, 0.02554468467443435, -0.5473637151080412, -0.016718404199258217]
        )
    }
]

答案 4 :(得分:0)

这是另一个脚本:

_list = [{'time': 1554283273.0824468, 'quaternion': [0.17855453309035293, 0.005453680566358193, -0.9834562739434834, -0.0300381977216707], 'id': 13762}
,{'time': 1554283273.1220775, 'quaternion': [-0.9705062538096711, -0.029642658768302424, -0.23913505674039495, -0.007304021853201095], 'id': 13762}
,{'time': 1554283273.1620576, 'quaternion': [0.836337807006856, 0.02554468467443435, -0.5473637151080412, -0.016718404199258217], 'id': 13762}]
_dict = {} 
tmp, qmp  =[], []     #initialize the lists
for d in _list:
    t, q = d['time'], d['quaternion']    #get the values of time and quaternion
    tmp.append(t)      #add the value to list t
    qmp.extend(q)       #extend means add the items of the list instead of the list itself
_dict['time'] = tmp     #assign the collected values to time
_dict['quaternion'] = qmp  #assign the collected values to quaternion
_dict