dict 和 set 的区别 (python)
Difference between dict and set (python)
所以,我知道这个,
a = {} # dict
构造一个空字典。现在,我也发现了这个,
b = {1, 2, 3} # set
创建一组。这很容易验证,因为,
>>>print(type(a))
<class 'dict'>
>>>print(type(b))
<class 'set'>
虽然我明白它的作用,但我不明白为什么 我们使用 'set notation' 来表示空字典。我试图在手册的 set
and dict
部分中找到有关此背后逻辑的更多信息,但遗憾的是,我一无所获。
谁能给我解释一下为什么我们要这样做?是出于历史原因,还是我遗漏了一些明显的东西?
语法不相同。字典首先使用花括号,然后您指定 key-value 对,其中键和值由冒号分隔:
>>> {'foo': 'bar'}
{'foo': 'bar'}
>>> type(_)
<type 'dict'>
集合后来被添加到语言中,{..}
花括号符号仅命名 元素 ,而不是对:
>>> {'foo'}
set(['foo'])
>>> type(_)
<type 'set'>
请注意,在 Python 2 中,解释器使用 set()
可调用对象回显对象。这也是您指定 empty 集合的方式:
>>> emptyset = set()
在 Python 3 中,回显对象时使用较新的 {..}
表示法,除非它为空:
>>> {'foo'}
{'foo'}
>>> _ - {'foo'} # difference, removing the one element
set()
set()
类型已添加到 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 中的 Python 语言。
Python中没有set literals 2,历史上花括号只用于字典。可以从列表(或任何可迭代对象)生成集合:
set([1, 2, 3])
set([i for i in range(1, 3)])
Python 3 引入了集合字面量和推导式(参见 PEP-3100),这使我们能够避免中间列表:
{1, 2, 3}
{i for i in range(1, 3)}
但是,由于向后兼容,空集形式是为字典保留的。来自 [Python-3000] sets in P3K? 的引用指出:
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).
following message 更好地描述了这些规则:
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.
{}
用于空字典而不是空集这一事实在很大程度上具有历史原因。字典的语法 {'a': 100, 'b': 200}
从 Python 开始就存在了。集合的语法 {1, 2, 3}
是在 Python 2.7 中引入的。由于 {}
已经使用了很长时间,它将作为定义空字典的方式保留下来。如果 Python 从一开始就有新的集合语法,可能会用 {}
定义一个空集合,用 {:}
.
定义一个空字典
所以,我知道这个,
a = {} # dict
构造一个空字典。现在,我也发现了这个,
b = {1, 2, 3} # set
创建一组。这很容易验证,因为,
>>>print(type(a))
<class 'dict'>
>>>print(type(b))
<class 'set'>
虽然我明白它的作用,但我不明白为什么 我们使用 'set notation' 来表示空字典。我试图在手册的 set
and dict
部分中找到有关此背后逻辑的更多信息,但遗憾的是,我一无所获。
谁能给我解释一下为什么我们要这样做?是出于历史原因,还是我遗漏了一些明显的东西?
语法不相同。字典首先使用花括号,然后您指定 key-value 对,其中键和值由冒号分隔:
>>> {'foo': 'bar'}
{'foo': 'bar'}
>>> type(_)
<type 'dict'>
集合后来被添加到语言中,{..}
花括号符号仅命名 元素 ,而不是对:
>>> {'foo'}
set(['foo'])
>>> type(_)
<type 'set'>
请注意,在 Python 2 中,解释器使用 set()
可调用对象回显对象。这也是您指定 empty 集合的方式:
>>> emptyset = set()
在 Python 3 中,回显对象时使用较新的 {..}
表示法,除非它为空:
>>> {'foo'}
{'foo'}
>>> _ - {'foo'} # difference, removing the one element
set()
set()
类型已添加到 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 中的 Python 语言。
Python中没有set literals 2,历史上花括号只用于字典。可以从列表(或任何可迭代对象)生成集合:
set([1, 2, 3])
set([i for i in range(1, 3)])
Python 3 引入了集合字面量和推导式(参见 PEP-3100),这使我们能够避免中间列表:
{1, 2, 3}
{i for i in range(1, 3)}
但是,由于向后兼容,空集形式是为字典保留的。来自 [Python-3000] sets in P3K? 的引用指出:
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).
following message 更好地描述了这些规则:
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.
{}
用于空字典而不是空集这一事实在很大程度上具有历史原因。字典的语法 {'a': 100, 'b': 200}
从 Python 开始就存在了。集合的语法 {1, 2, 3}
是在 Python 2.7 中引入的。由于 {}
已经使用了很长时间,它将作为定义空字典的方式保留下来。如果 Python 从一开始就有新的集合语法,可能会用 {}
定义一个空集合,用 {:}
.