将flat dict转换为嵌套列表和dict并将其相交

时间:2014-07-01 05:20:00

标签: python list dictionary

在我的数据库中从类别中选择查询后,结果如下:

tags=[
    { id: 1, post_id: 155, term: 'python'},
    { id: 2, post_id: 155, term: 'c'},
    { id: 3, post_id: 155, term: 'php'},
    { id: 4, post_id: 145, term: 'php'},
    { id: 5, post_id: 145, term: 'python'},
    { id: 7, post_id: 145, term: 'c'},
    { id: 9, post_id: 145, term: 'java'},
    { id: 12, post_id: 166, term: 'python'},
    { id: 15, post_id: 166, term: 'php'}
    ]

我希望代码将此dict列表转换为:

tags={
     155:['python','c', 'php'],
     145:['php','python','c','java'],
     166:['python','php']
     }

和这样的交集:

tags={155:['python', 'php'], 145:['python', 'php'], 166:['python','php']}

如何用python做到这一点?

我的代码不正确:

tags = Tag.query.filter(Tag.post_id.in_(ids)).all()

terms = dict()
for tag in tags:
    terms[tag.post_id].append(tag.term)

2 个答案:

答案 0 :(得分:1)

from collections import defaultdict                          
tags=[                                                       
        { "id": 1, "post_id": 155, "term": 'python'},        
        { "id": 2, "post_id": 155, "term": 'c'},             
        { "id": 3, "post_id": 155, "term": 'php'},           
        { "id": 4, "post_id": 145, "term": 'php'},           
        { "id": 5, "post_id": 145, "term": 'python'},        
        { "id": 7, "post_id": 145, "term": 'c'},             
        { "id": 9, "post_id": 145, "term": 'java'},          
        { "id": 12, "post_id": 166, "term": 'python'},       
        { "id": 15, "post_id": 166, "term": 'php'}           
        ]                                                    

formatted_tags = defaultdict(list)                           
for tag in tags:                                             
    formatted_tags[tag["post_id"]].append(tag["term"])       

print dict(formatted_tags)                                   
intersection = set(formatted_tags[formatted_tags.keys()[0]]) 
for tag in formatted_tags:                                   
    intersection = intersection & set(formatted_tags[tag])   

intersection_tags = {}                                       
intersection = list(intersection)                            
for tag in formatted_tags:                                   
    intersection_tags[tag] = intersection                    
print intersection_tags

<强>输出

{145: ['php', 'python', 'c', 'java'], 155: ['python', 'c', 'php'], 166: ['python', 'php']}
{145: ['python', 'php'], 155: ['python', 'php'], 166: ['python', 'php']}

答案 1 :(得分:0)

tags=[
{ 'id': 1, 'post_id': 155, 'term': 'python'},
{ 'id': 2, 'post_id': 155, 'term': 'c'},
{ 'id': 3, 'post_id': 155, 'term': 'php'},
{ 'id': 4, 'post_id': 145, 'term': 'php'},
{ 'id': 5, 'post_id': 145, 'term': 'python'},
{ 'id': 7, 'post_id': 145, 'term': 'c'},
{'id': 9, 'post_id': 145, 'term': 'java'},
{ 'id': 12, 'post_id': 166, 'term': 'python'},
{ 'id': 15, 'post_id': 166, 'term': 'php'}
]



result={}

for val in tags:
    if val['post_id'] in result.keys():
        aList=result[val['post_id']]
        aList.append(val['term'])
    else:
        result[val['post_id']]=[val['term']]

print(result)

<强>输出:

{145: ['php', 'python', 'c', 'java'], 155: ['python', 'c', 'php'], 166: 
['python', 'php']}