I have below input list of 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}]};
Am trying to get the count of id's for each cid along with the id values, I used below code for that -
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 the output as -
[{'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 the combination of the categories {66,27,29} ?
I tried using below approach for getting the combinations of this input - it gives the combination of items from the 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 couldn't figure out how can I use this approach to get me the result as below for categories {66,27,29} from the '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 please suggest on how I can achieve this?