Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a "dictionary of sets"

I need to efficiently store data in something that would resemble a "dictionary of sets" e.g. have a dictionary with multiple (unique) values matching each unique key. The source of my data would be a (not very well) structured XML.

My idea is: I will look through a number of elements and find keys. If the key does not exist, add it to dictionary, if it already exists, just add a new value in the corresponding key.

And the result would be something like:

{
 'key1': {'1484', '1487', 1488', ...}
 'key2': {'1485', '1486', '1489', ...}
 'key3': {'1490', '1491', '1492', ...}
 ...
}

I need to add new keys on the go. I need to push unique values into each set. I need to be able to iterate through the whole dictionary.

I am not sure if this is even feasible, but if anybody could push me in the right direction, I would be more than thankful.

like image 356
Stanislav Pavlovič Avatar asked Feb 23 '17 19:02

Stanislav Pavlovič


People also ask

Can I put a set in a dictionary Python?

Sets being mutable are not hashable, so they can't be used as dictionary keys. On the other hand, frozen sets are hashable and can be used as keys to a dictionary. This datatype supports methods like copy(), difference(), intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference() and union().

Can sets be used as dictionary keys?

Yes. According to the docs, Frozenset is hashable because it's immutable. This would imply that it can be used as the key to a dict, because the prerequisite for a key is that it is hashable.

Can you create a dictionary from a list?

To convert a list to a dictionary using the same values, you can use the dict. fromkeys() method. To convert two lists into one dictionary, you can use the Python zip() function. The dictionary comprehension lets you create a new dictionary based on the values of a list.


1 Answers

I'm not going to benchmark this but in my experience native dicts are faster

store = {}
for key, value in yoursource:
    try:
        store[key].add(value)
    except KeyError:
        store[key] = {value}
like image 182
Paul Panzer Avatar answered Oct 05 '22 23:10

Paul Panzer