问题
Can anybody tell me how the internal implementation of set and dict is different in python? Do they use the same data structure in the background?
++ In theory, one can use dict to achieve set functionality.
回答1:
In CPython, sets and dicts use the same basic data structure. Sets tune it slightly differently, but it is basically a hash table just like dictionaries.
You can take a look at the implementation details in the C code: setobject.c and dictobject.c; the implementations are very close; the setobject.c
implementation was started as a copy of dictobject.c
originally. dictobject.c
has more implementation notes and tracing calls, but the actual implementations of the core functions differs only in details.
The most obvious difference is that the keys in the hash table are not used to reference values, like in dictionaries, so a setentry struct only has a cached hash and key, the dictentry struct adds a value pointer.
Before we had the built-in set
, we had the sets module, a pure-Python implementation that used dict
objects to track the set values as keys. And in Python versions before the sets
module was available, we did just that: use dict
objects with keys as the set values, to track unique, unordered values.
回答2:
These two are use the same datastructure in the backend. e.g in sets you cannot store duplicate values but in dict you can store multople same values and you can turn the dict to sets by changing the behavior of dict
来源:https://stackoverflow.com/questions/24159713/difference-between-python-set-and-dict-internally