I'm learning python, and I have a novice question about initializing sets. Through testing, I've discovered that a set can be initialized like so:
我正在学习python,我有一个关于初始化集的新手问题。通过测试,我发现可以像这样初始化一个集合:
my_set = {'foo', 'bar', 'baz'}
Are there any disadvantages of doing it this way, as opposed to the standard way of:
这样做是否有任何缺点,与标准方式相反:
my_set = set(['foo', 'bar', 'baz'])
or is it just a question of style?
还是只是一个风格问题?
3 个解决方案
#1
60
There are two obvious issues with the set literal syntax:
set字面语法有两个明显的问题:
my_set = {'foo', 'bar', 'baz'}
-
It's not available before Python 2.7
它在Python 2.7之前不可用
-
There's no way to express an empty set using that syntax (using
{}
creates an empty dict)使用该语法无法表达空集(使用{}创建一个空字典)
Those may or may not be important to you.
那些对你来说可能重要,也可能不重要。
The section of the docs outlining this syntax is here.
这里概述了这种语法的文档部分。
#2
33
Compare also the difference between {}
and set()
with a single word argument.
比较{}和set()与单个单词参数之间的区别。
>>> a = set('aardvark')
>>> a
{'d', 'v', 'a', 'r', 'k'}
>>> b = {'aardvark'}
>>> b
{'aardvark'}
but both a
and b
are sets of course.
但是a和b都是套路。
#3
18
From Python 3 documentation (the same holds for python 2.7):
从Python 3文档(对于python 2.7同样适用):
Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.
大括号或set()函数可用于创建集合。注意:要创建一个空集,您必须使用set(),而不是{};后者创建一个空字典,一个我们将在下一节讨论的数据结构。
in python 2.7:
在python 2.7中:
>>> my_set = {'foo', 'bar', 'baz', 'baz', 'foo'}
>>> my_set
set(['bar', 'foo', 'baz'])
Be aware that {}
is also used for map
/dict
:
请注意,{}也用于map / dict:
>>> m = {'a':2,3:'d'}
>>> m[3]
'd'
>>> m={}
>>> type(m)
<type 'dict'>
One can use sets comprehension:
可以使用集合理解:
>>> a = {x for x in """didn't know about {} and sets """ if x not in 'set' }
>>> a
set(['a', ' ', 'b', 'd', "'", 'i', 'k', 'o', 'n', 'u', 'w', '{', '}'])
#1
60
There are two obvious issues with the set literal syntax:
set字面语法有两个明显的问题:
my_set = {'foo', 'bar', 'baz'}
-
It's not available before Python 2.7
它在Python 2.7之前不可用
-
There's no way to express an empty set using that syntax (using
{}
creates an empty dict)使用该语法无法表达空集(使用{}创建一个空字典)
Those may or may not be important to you.
那些对你来说可能重要,也可能不重要。
The section of the docs outlining this syntax is here.
这里概述了这种语法的文档部分。
#2
33
Compare also the difference between {}
and set()
with a single word argument.
比较{}和set()与单个单词参数之间的区别。
>>> a = set('aardvark')
>>> a
{'d', 'v', 'a', 'r', 'k'}
>>> b = {'aardvark'}
>>> b
{'aardvark'}
but both a
and b
are sets of course.
但是a和b都是套路。
#3
18
From Python 3 documentation (the same holds for python 2.7):
从Python 3文档(对于python 2.7同样适用):
Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.
大括号或set()函数可用于创建集合。注意:要创建一个空集,您必须使用set(),而不是{};后者创建一个空字典,一个我们将在下一节讨论的数据结构。
in python 2.7:
在python 2.7中:
>>> my_set = {'foo', 'bar', 'baz', 'baz', 'foo'}
>>> my_set
set(['bar', 'foo', 'baz'])
Be aware that {}
is also used for map
/dict
:
请注意,{}也用于map / dict:
>>> m = {'a':2,3:'d'}
>>> m[3]
'd'
>>> m={}
>>> type(m)
<type 'dict'>
One can use sets comprehension:
可以使用集合理解:
>>> a = {x for x in """didn't know about {} and sets """ if x not in 'set' }
>>> a
set(['a', ' ', 'b', 'd', "'", 'i', 'k', 'o', 'n', 'u', 'w', '{', '}'])