汇总列表中的信息

时间:2018-06-29 13:54:05

标签: python python-3.x list dictionary

假设我有一个列表:

INPUT=[{'id': 1, 'context': 'this is q1', 'content': 'this is ch1'},
{'id': 1, 'context': 'this is q1', 'content': 'this is ch2'},
{'id': 1, 'context': 'this is q1', 'content': 'this is ch3'}, 
{'id': 2, 'context': 'this is q12', 'content': 'this is ch14'}]

转换:

OUTPUT=[{'id': 1, 'context': 'this is q1', 'content': ['this is ch1','this is ch2','this is ch3']},

{'id': 2, 'context': 'this is q12', 'content': 'this is ch14'}]

python上执行上述转换的最简单方法是什么?

3 个答案:

答案 0 :(得分:1)

使用itertools.groupby

演示:

from itertools import groupby

INPUT=[{'id': 1, 'context': 'this is q1', 'content': 'this is ch1'},
{'id': 1, 'context': 'this is q1', 'content': 'this is ch2'},
{'id': 1, 'context': 'this is q1', 'content': 'this is ch3'}, 
{'id': 2, 'context': 'this is q12', 'content': 'this is ch14'}]

res = []
for key, group in groupby(INPUT, lambda x: (x["id"])):
    temp = []
    for thing in group:
        temp.append(thing["content"])
    res.append({"id": key, "context": thing["context"], 'content': temp})
print(res)

输出:

[{'content': ['this is ch1', 'this is ch2', 'this is ch3'], 'id': 1, 'context': 'this is q1'}, {'content': ['this is ch14'], 'id': 2, 'context': 'this is q12'}]

答案 1 :(得分:0)

我建议您采用以下简短易懂的解决方案:

from collections import defaultdict

INPUT=[
    {'id': 1, 'context': 'this is q1', 'content': 'this is ch1'},
    {'id': 1, 'context': 'this is q1', 'content': 'this is ch2'},
    {'id': 1, 'context': 'this is q1', 'content': 'this is ch3'}, 
    {'id': 2, 'context': 'this is q12', 'content': 'this is ch14'}]

# Create first a dictionary:
#     - with tuples (id,context) as key
#     - with the appended contents as value
newDict = defaultdict(list)
for d in INPUT:
    newDict[(d['id'],d['context'])].append(d['content'])

# Convert this dictionary into a list of dictionaries with expected keys
OUTPUT = [{'id':k[0], 'context':k[1], 'content':v} for k,v in newDict.items()]

print(OUTPUT)
# [{'id': 1, 'context': 'this is q1', 'content': ['this is ch1', 'this is ch2', 'this is ch3']},
#  {'id': 2, 'context': 'this is q12', 'content': ['this is ch14']}]

答案 2 :(得分:0)

尝试一下。

for i in range(len(INPUT)):
    INPUT[i]['content'] = [INPUT[i]['content']]
    print(INPUT[i]['content'])