dict和set之间的区别(python)

时间:2021-08-13 13:28:18

So, I know that this,

所以,我知道这个,

a = {}  # dict

Constructs an empty dictionary. Now, I also picked up that this,

构造一个空字典。现在,我也接受了这个,

b = {1, 2, 3}  # set

Creates a set. This can easily be verified, as,

创建一个集合。这很容易验证,因为,

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

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

While I understand what it does, I fail to see why we use the same syntax for both sets and dictionaries. I tried to find some more information about the logic behind this in the set and dict sections of the manual, but sadly, I got nothing out of it.

虽然我理解它的作用,但我不明白为什么我们为集合和字典使用相同的语法。我试图在手册的set和dict部分找到关于这背后的逻辑的更多信息,但遗憾的是,我什么也没有得到它。

Could anyone explain to me why we do this in this way? Is it for historical reasons, or am I missing something blatantly obvious?

任何人都可以向我解释为什么我们这样做?这是出于历史原因,还是我错过了一些明显的东西?

3 个解决方案

#1


24  

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

Python 2中没有设置文字,历史上只用于字典的花括号。可以从列表(或任何迭代)生成集合:

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

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

Python 3引入了集合文字和理解(参见PEP-3100),它允许避免使用中间列表:

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

Empty set form, however reserved for dictionaries as backward compatibility. Check references from PEP-3100, i.e. here: [Python-3000] sets in P3K?

空集表单,但是为字典保留为向后兼容性。检查来自PEP-3100的参考文献,即这里:P3K中的[Python-3000]设置?

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

我确信我们可以解决一些问题 - 我同意,{}对于空集和{:}对于空dict是理想的,如果不是为了向后兼容。当我第一次写PEP时,我喜欢“特殊的空对象”的想法(即,有{}可以变成一个集合或字典)​​,但是这里的一位教师说服我只会导致混淆新人的想法(以及实施的痛苦)。

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.

我认为Guido有最好的解决方案。对空集使用set(),对空集使用{},对集合理解/显示使用{genexp},对显式集合文字使用{1,2,3},并使用{k1:v1,k2:v2}字面文字。如果需求超过厌恶,我们可以随后添加{/}。

#2


9  

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:

请注意,在Python 2中,解释器使用set()可调用来回显对象。这也是你如何指定一个空集:

>>> emptyset = set()

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

在Python 3中,在回显对象时使用较新的{..}表示法,除非它是空的:

>>> {'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.

set()类型被添加到2.4版本的Python语言中(参见PEP 218),set 3的大括号语法在Python 3中添加并反向移植到Python 2.7。

#3


6  

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 {:}.

{}用于空字典而不用于空集的事实在很大程度上是历史原因。自Python开始以来,词典的语法{'a':100,'b':200}已经存在。 Python 2.7引入了集合的语法{1,2,3}。由于{}已经使用了这么长时间,因此它将作为定义空字典的方式。如果Python从一开始就有新的集合语法,那么可能会使用{}和带有{:}的空字典定义空集。

#1


24  

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

Python 2中没有设置文字,历史上只用于字典的花括号。可以从列表(或任何迭代)生成集合:

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

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

Python 3引入了集合文字和理解(参见PEP-3100),它允许避免使用中间列表:

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

Empty set form, however reserved for dictionaries as backward compatibility. Check references from PEP-3100, i.e. here: [Python-3000] sets in P3K?

空集表单,但是为字典保留为向后兼容性。检查来自PEP-3100的参考文献,即这里:P3K中的[Python-3000]设置?

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

我确信我们可以解决一些问题 - 我同意,{}对于空集和{:}对于空dict是理想的,如果不是为了向后兼容。当我第一次写PEP时,我喜欢“特殊的空对象”的想法(即,有{}可以变成一个集合或字典)​​,但是这里的一位教师说服我只会导致混淆新人的想法(以及实施的痛苦)。

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.

我认为Guido有最好的解决方案。对空集使用set(),对空集使用{},对集合理解/显示使用{genexp},对显式集合文字使用{1,2,3},并使用{k1:v1,k2:v2}字面文字。如果需求超过厌恶,我们可以随后添加{/}。

#2


9  

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:

请注意,在Python 2中,解释器使用set()可调用来回显对象。这也是你如何指定一个空集:

>>> emptyset = set()

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

在Python 3中,在回显对象时使用较新的{..}表示法,除非它是空的:

>>> {'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.

set()类型被添加到2.4版本的Python语言中(参见PEP 218),set 3的大括号语法在Python 3中添加并反向移植到Python 2.7。

#3


6  

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 {:}.

{}用于空字典而不用于空集的事实在很大程度上是历史原因。自Python开始以来,词典的语法{'a':100,'b':200}已经存在。 Python 2.7引入了集合的语法{1,2,3}。由于{}已经使用了这么长时间,因此它将作为定义空字典的方式。如果Python从一开始就有新的集合语法,那么可能会使用{}和带有{:}的空字典定义空集。