Difficulty getting a count item for combining a list of items from a python dictionary

I have a list of input dictionaries

inpdata =   {"cat": [{"categories": [{"cid": 27}, {"cid": 66}, {"cid": 29}], "id": 20}, 
                     {"categories": [{"cid": 66}], "id": 21}, 
                     {"categories": [{"cid": 66}, {"cid": 27}], "id": 22}, 
                     {"categories": [{"cid": 66}, {"cid": 27}], "id": 23}, 
                     {"categories": [{"cid": 66}, {"cid": 29}, {"cid": 27}], "id": 24}]};

I am trying to get id counter for each cid along with id values, I used below code for this -

allcategories = set( sec['cid'] for record in inpdata['cat'] for sec in record['categories'] )
summarize = lambda record: record['id']   
fs_cat = [
        {
            'cat':cid,
            'count':len(matches),
            'ids':[ summarize( match ) for match in matches ]
        }
        for cid in allcategories
        for matches in [[
            record for record in inpdata['cat'] if cid in [ sec['cid'] for sec in record['categories'] ]
        ]]
    ]
print(fs_cat)

This gives a result like -

[{'cat': 66, 'count': 5, 'ids': [20, 21, 22, 23, 24]},
 {'cat': 27, 'count': 4, 'ids': [20, 22, 23, 24]},
 {'cat': 29, 'count': 2, 'ids': [20, 24]}
 ]

But how can I get a combination of categories {66,27,29}?

I tried using the approach below to get combinations of this input - it gives a combination of items from a list

allcategories = {66,27,29}
for subset in itertools.chain.from_iterable(itertools.combinations(allcategories, n) for n in range(len(allcategories) + 1)):
    print(subset)

But I could not understand how I can use this approach to get the result, as shown below for the categories {66,27,29} from 'inpdata'

result=[{'cat': '66', 'count': 5, 'ids': [20, 21, 22, 23, 24]},
        {'cat': '27', 'count': 4, 'ids': [20, 22, 23, 24]},
        {'cat': '29', 'count': 2, 'ids': [20, 24]},
        {'cat': '66&27', 'count': 4, 'ids': [20, 22, 23, 24]},
        {'cat': '66&29', 'count': 2, 'ids': [20, 24]},
        {'cat': '27&29', 'count': 2, 'ids': [20, 24]},
        {'cat': '66&27&29', 'count': 2, 'ids': [20, 24]}
        ]

Could you suggest how I can achieve this?

+4
source share
1 answer

itertools.combinations(1), itertools.combinations(2),... upto itertools.combinations(n) fs_cat (, n = len(fs_cat))

import itertools
import operator
from functools import reduce

fs_cat = [
    {'cat': 66, 'count': 5, 'ids': [20, 21, 22, 23, 24]},
    {'cat': 27, 'count': 4, 'ids': [20, 22, 23, 24]},
    {'cat': 29, 'count': 2, 'ids': [20, 24]},
]

result = []
for n in range(1, len(fs_cat) + 1):  # 1, 2, ..., len(fs_cat)
    for xs in itertools.combinations(fs_cat, n):
        cat = '&'.join(map(str, sorted(x['cat'] for x in xs)))
        ids = sorted(reduce(operator.and_, (set(x['ids']) for x in xs)))
        result.append({'cat': cat, 'count': len(ids), 'ids': ids})

>>> result
[{'cat': '66', 'count': 5, 'ids': [20, 21, 22, 23, 24]},
 {'cat': '27', 'count': 4, 'ids': [20, 22, 23, 24]},
 {'cat': '29', 'count': 2, 'ids': [20, 24]},
 {'cat': '27&66', 'count': 4, 'ids': [20, 22, 23, 24]},
 {'cat': '29&66', 'count': 2, 'ids': [20, 24]},
 {'cat': '27&29', 'count': 2, 'ids': [20, 24]},
 {'cat': '27&29&66', 'count': 2, 'ids': [20, 24]}]
+2

Source: https://habr.com/ru/post/1620110/


All Articles