Dict 和 set (python)之间的区别

所以我知道,

a = {}  # dict

构建一个空字典。现在,我还发现这个,

b = {1, 2, 3}  # set

创建一个集合。这可以很容易地验证,

>>>print(type(a))
<class 'dict'>


>>>print(type(b))
<class 'set'>

虽然我知道它是做什么的,但是我没有看到 为什么我们对空字典使用“ set 表示法”。我试图在手册的 setdict章节中找到更多关于这背后的逻辑的信息,但遗憾的是,我什么也没有得到。

有人能解释一下为什么我们要这么做吗?是因为历史原因,还是我遗漏了什么显而易见的东西?

103355 次浏览

The syntax is not the same. Dictionaries used curly braces the first and you specify key-value pairs, where the key and value are separated by a colon:

>>> {'foo': 'bar'}
{'foo': 'bar'}
>>> type(_)
<type 'dict'>

Sets were added to the language later on, and the {..} curly brace notation only names elements, not pairs:

>>> {'foo'}
set(['foo'])
>>> type(_)
<type 'set'>

Note that in Python 2, the interpreter echoes the object using the set() callable. That's also how you specify an empty set:

>>> emptyset = set()

In Python 3, the newer {..} notation is used when echoing the object, unless it is empty:

>>> {'foo'}
{'foo'}
>>> _ - {'foo'}  # difference, removing the one element
set()

The set() type was added to the Python language in version 2.4 (see PEP 218), the curly brace syntax for set literals was added in Python 3 and back-ported to Python 2.7.

There were no set literals in Python 2, historically curly braces were only used for dictionaries. Sets could be produced from lists (or any iterables):

set([1, 2, 3])
set([i for i in range(1, 3)])

Python 3 introduced set literals and comprehensions (see PEP-3100) which allowed us to avoid intermediate lists:

{1, 2, 3}
{i for i in range(1, 3)}

The empty set form, however, was reserved for dictionaries due to backwards compatibility. References from [Python-3000] sets in P3K? states:

I'm sure we can work something out --- I agree, {} for empty set and {:} for empty dict would be ideal, were it not for backward compatibility. I liked the "special empty object" idea when I first wrote the PEP (i.e., have {} be something that could turn into either a set or dict), but one of the instructors here convinced me that it would just lead to confusion in newcomers' minds (as well as being a pain to implement).

The following message describes these rules better:

I think Guido had the best solution. Use set() for empty sets, use {} for empty dicts, use {genexp} for set comprehensions/displays, use {1,2,3} for explicit set literals, and use {k1:v1, k2:v2} for dict literals. We can always add {/} later if demand exceeds distaste.

The fact that {} is used for an empty dictionary and not for an empty set has largely historical reasons. The syntax {'a': 100, 'b': 200} for dictionaries has been around since the beginning of Python. The syntax {1, 2, 3} for sets was introduced with Python 2.7. Since {} has been used for such a long time it will stay as the way to define an empty dictionary. If Python would have had the new set syntax since the beginning, likely an empty set would be defined with {} and an empty dictionary with {:}.

A set is an unordered collection. A dictionary is an unordered collection of data that stores data in key-value pairs. Dictionaries and sets use hash tables in order to achieve their O(1) lookup and insertions. Data-structures documentation to refer: https://docs.python.org/3/tutorial/datastructures.html

enter image description here