空集文字?
[]
= 空 列表
()
= 空 元组
{}
= 空 < code>dict
空的 set
是否有类似的表示法? 或者我必须编写set()
?
[]
= empty list
()
= empty tuple
{}
= empty dict
Is there a similar notation for an empty set
?
Or do I have to write set()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,空集没有文字语法。您必须编写
set()
。No, there's no literal syntax for the empty set. You have to write
set()
.无论如何,请使用
set()
创建一个空集。但是,如果您想给人们留下深刻的印象,请告诉他们您可以使用文字和
*
以及 Python >= 3.5 (参见 PEP 448) 通过这样做:这基本上是一种更简洁的方式
{_ for _ in ()}
,但是,不要这样做。By all means, please use
set()
to create an empty set.But, if you want to impress people, tell them that you can create an empty set using literals and
*
with Python >= 3.5 (see PEP 448) by doing:this is basically a more condensed way of doing
{_ for _ in ()}
, but, don't do this.只是为了扩展已接受的答案:
从版本
2.7
和3.1
python 得到了set
文字{}
形式用法{1,2,3}
,但{}
本身仍然用于空字典。Python 2.7(第一行在 Python <2.7 中无效)
Python 3.x
更多信息:https://docs.python.org/3/whatsnew/2.7.html#other-language-changes
Just to extend the accepted answer:
From version
2.7
and3.1
python has gotset
literal{}
in form of usage{1,2,3}
, but{}
itself still used for empty dict.Python 2.7 (first line is invalid in Python <2.7)
Python 3.x
More here: https://docs.python.org/3/whatsnew/2.7.html#other-language-changes
是的。适用于非空字典/集合的相同符号也适用于空字典/集合。
请注意非空
dict
和set
文字之间的区别:{1: 'a', 2: 'b', 3: 'c'} -- 内部的多个键值对组成一个
dict
{'aaa', 'bbb', 'ccc'}
-- 内部的多个值构成一个集合
因此:
{}
==零个键值对 == 空dict
{*()}
== 空值元组 == 空set
然而,事实上,您可以这样做,并不意味着您应该这样做。除非你有一些强有力的理由,否则最好显式构造一个空集,例如:
注意:正如ctrueden在评论中注意到的那样,
{()}
不是一个空集。它是一个包含 1 个元素的集合:空元组。Yes. The same notation that works for non-empty dict/set works for empty ones.
Notice the difference between non-empty
dict
andset
literals:{1: 'a', 2: 'b', 3: 'c'}
-- a number of key-value pairs inside makes adict
{'aaa', 'bbb', 'ccc'}
-- a number of values inside makes aset
So:
{}
== zero number of key-value pairs == emptydict
{*()}
== empty tuple of values == emptyset
However the fact, that you can do it, doesn't mean you should. Unless you have some strong reasons, it's better to construct an empty set explicitly, like:
NB: As ctrueden noticed in comments,
{()}
is not an empty set. It's a set with 1 element: empty tuple.这取决于您是否需要文字进行比较或赋值。
如果您想将现有集合清空,可以使用
.clear()
方法,特别是如果您想避免创建新对象。如果要进行比较,请使用set()
或检查长度是否为0。例如:
It depends on if you want the literal for a comparison, or for assignment.
If you want to make an existing set empty, you can use the
.clear()
metod, especially if you want to avoid creating a new object. If you want to do a comparison, useset()
or check if the length is 0.example:
更疯狂的想法是:Python 3 接受 unicode 标识符,您可以声明一个变量
phi = freezeset()
(phi 是 U+03D5)并使用它。Adding to the crazy ideas: with Python 3 accepting unicode identifiers, you could declare a variable
ϕ = frozenset()
(ϕ is U+03D5) and use it instead.在 Python 中创建空 Set 有几种方法:
这是 python 中的内置方法,用于在该变量中创建空集。
请参阅此示例:
集={"嗨","怎么样","是","你","全部"}
类型(套) (这条线
输出:设置)
set.clear()
print(sets) (此行输出:{})
类型(套) (这条线
输出:set)
因此,这是创建空 Set 的两种方法。
There are few ways to create empty Set in Python :
This is the built-in method in python that creates Empty set in that variable.
See this Example:
sets={"Hi","How","are","You","All"}
type(sets) (This Line
Output : set)
sets.clear()
print(sets) (This Line Output : {})
type(sets) (This Line
Output : set)
So, This are 2 ways to create empty Set.