集的词典的元素在Python的

时间:2014-09-02 20:00:53

标签: python dictionary set

我有这样的字典:

dict1 = {0: set([1, 4, 5]), 1: set([2, 6]), 2: set([3]), 3: set([0]), 4: set([1]), 5: set([2]), 6: set([])}

从这个字典中我想构建另一个字典来计算dict1中每个其他值的键的出现次数,结果应该是:

result_dict = {0: 1, 1: 2, 2: 2, 3: 1, 4: 1, 5: 1, 6: 1}

我的代码是这样的:

dict1 =  {0: set([1, 4, 5]), 1: set([2, 6]), 2: set([3]), 3: set([0]), 4: set([1]), 5:set([2]), 6: set([])}
result_dict = {}
for pair in dict1.keys():
    temp_dict = list(dict1.keys())

    del temp_dict[pair]
    count = 0
    for other_pairs in temp_dict :
        if pair in dict1[other_pairs]:
            count = count + 1
    result_dict[pair] = count  

此代码的问题在于,对于大量数据,它非常慢。 另一种尝试是一行,如下:

result_dict = dict((key ,dict1.values().count(key)) for key in dict1.keys())  

但它给了我错误的结果,因为dict1的值是集:

{0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}

提前多多感谢

2 个答案:

答案 0 :(得分:1)

我想,对于第一次尝试,我会找出哪些值:

all_values = set().union(*dict1.values())

然后我会尝试计算每个值发生的次数:

result_dict = {}
for v in all_values:
    result_dict[v] = sum(v in dict1[key] for key in dict1)

另一种方法是使用collections.Counter

result_dict = Counter(v for set_ in dict1.values() for v in set_)

这可能比我的第一个解决方案“更清洁” - 但它确实涉及嵌套理解,这可能有点难以理解。它确实有效:

>>> from collections import Counter
>>> dict1
{0: set([1, 4, 5]), 1: set([2, 6]), 2: set([3]), 3: set([0]), 4: set([1]), 5: set([2]), 6: set([])}
>>> result_dict = Counter(v for set_ in dict1.values() for v in set_)

答案 1 :(得分:0)

只需使用dict1中的密钥创建第二个字典,并在0处启动值。然后迭代dict1集合中的值,随时增加result_dict的值。运行时为 O(n),其中 n dict1集合中的值的总数。

dict1 = {0: set([1, 4, 5]), 1: set([2, 6]), 2: set([3]), 3: set([0]), 4: set([1]), 5:set([2]), 6: set([])}
result_dict = dict.fromkeys(dict1.keys(), 0)
# {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}

for i in dict1.keys():
    for j in dict1[i]: 
        result_dict[j] += 1

print result_dict
# {0: 1, 1: 2, 2: 2, 3: 1, 4: 1, 5: 1, 6: 1}