简体   繁体   中英

Elements of dict of sets in python

I have a dictionary like this:

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

and from this dictionary I want to build another dictionary that count the occurrences of keys in dict1 in every other value ,that is the results should be:

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

My code was this :

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  

The problem with this code is that it is very slow with large set of data. Another attempt was in a single line, like this :

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

but it gives me wrong results, since values of dict1 are sets:

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

thanks a lot in advance

I suppose, for a first stab, I would figure out which values are there:

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

Then I'd try to count how many times each value occurred:

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

Another approach would be to use a collections.Counter :

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

This is probably "cleaner" than my first solution -- but it does involve a nested comprehension which can be a little difficult to grok. It does work however:

>>> 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_)

Just create a second dictionary using the keys from dict1 , with values initiated at 0 . Then iterate through the values in the sets of dict1 , incrementing values of result_dict as you go. The runtime is O(n) , where n is the aggregate number of values in sets of 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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM