dict 和 set 的区别(python)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34370599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between dict and set (python)
提问by N. Wouda
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 whywe 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?
谁能向我解释为什么我们这样做?是出于历史原因,还是我遗漏了一些显而易见的东西?
采纳答案by myaut
There were no set literalsin Python 2, historically curly braces were 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 allowed us to avoid intermediate lists:
Python 3 引入了集合文字和推导式(参见PEP-3100),这使我们能够避免中间列表:
{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:
然而,由于向后兼容,空集形式被保留用于字典。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).
我相信我们可以解决一些问题 --- 我同意, 如果不是为了向后兼容,
{}
空集和{:}
空字典是理想的。当我第一次写 PEP 时,我喜欢“特殊的空对象”的想法(即,有{}
可以变成集合或字典的东西),但这里的一位讲师说服我,这只会导致新人的困惑思想(以及实施起来很痛苦)。
The following messagedescribes 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}
的字典文字。如果需求超过厌恶,我们可以随时添加{
/}
稍后。
回答by Martijn Pieters
The syntax is notthe same. Dictionaries used curly braces the first and you specify key-valuepairs, 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 emptyset:
请注意,在 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 3and back-ported to Python 2.7.
该set()
类型在2.4 版中添加到 Python 语言中(请参阅PEP 218),集合文字的花括号语法在 Python 3中添加并反向移植到 Python 2.7 中。
回答by Mike Müller
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': 100, 'b': 200}
字典的语法从 Python 开始就已经存在。{1, 2, 3}
集合的语法是在 Python 2.7 中引入的。由于{}
已经使用了这么长时间,它将继续作为定义空字典的方式。如果 Python 从一开始就拥有新的集合语法,则很可能会{}
使用{:}
.