Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
难以从python字典中获取项目组合的项目计数_Python_Python 3.x - Fatal编程技术网

难以从python字典中获取项目组合的项目计数

难以从python字典中获取项目组合的项目计数,python,python-3.x,Python,Python 3.x,我有下面的字典输入列表 inpdata = {"cat": [{"categories": [{"cid": 27}, {"cid": 66}, {"cid": 29}], "id": 20}, {"categories": [{"cid": 66}], "id": 21}, {"categories": [{"cid": 66}, {"cid": 27}], "id": 22},

我有下面的字典输入列表

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}]};
我试图获得每个cid的id计数以及id值,我使用了下面的代码-

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)
这将输出为-

[{'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]}
 ]
但是我怎样才能得到类别{66,27,29}的组合呢

我试着使用下面的方法来获得这些输入的组合——它给出了列表中项目的组合

allcategories = {66,27,29}
for subset in itertools.chain.from_iterable(itertools.combinations(allcategories, n) for n in range(len(allcategories) + 1)):
    print(subset)
但是我不知道如何使用这种方法从“inpdata”中得到以下类别{66,27,29}的结果

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]}
        ]
您能建议我如何实现这一点吗?

itertools.compositions(2)
。。。最多
itertools。组合(n)
将为您提供
fs\u cat
的所有组合(其中,n=
len(fs\u cat)


非常感谢falsetru:)我觉得,这是解决这类问题的一种先进方法。非常感谢你。
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]}]