问题:空集文字?
[]
=空 list
()
=空 tuple
{}
=空 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()
?
回答 0
No, there’s no literal syntax for the empty set. You have to write set()
.
回答 1
一定请使用 set()
创建一个空集。
但是,如果您想打动别人,请告诉他们您可以使用文字和*
Python> = 3.5(请参阅PEP 448)创建一个空集,方法是:
>>> s = {*()} # or {*{}} or {*[]}
>>> print(s)
set()
这基本上是一种更简洁的方法{_ 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:
>>> s = {*()} # or {*{}} or {*[]}
>>> print(s)
set()
this is basically a more condensed way of doing {_ for _ in ()}
, but, don’t do this.
回答 2
只是为了扩展公认的答案:
从version 2.7
和3.1
python起,set
文字已经{}
以用法的形式出现了{1,2,3}
,但是{}
它本身仍然用于空字典。
Python 2.7(第一行在Python <2.7中无效)
>>> {1,2,3}.__class__
<type 'set'>
>>> {}.__class__
<type 'dict'>
Python 3.x
>>> {1,4,5}.__class__
<class 'set'>
>>> {}.__class__
<type 'dict'>
此处更多内容:https://docs.python.org/3/whatsnew/2.7.html#other-language-changes
Just to extend the accepted answer:
From version 2.7
and 3.1
python has got set
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)
>>> {1,2,3}.__class__
<type 'set'>
>>> {}.__class__
<type 'dict'>
Python 3.x
>>> {1,4,5}.__class__
<class 'set'>
>>> {}.__class__
<type 'dict'>
More here: https://docs.python.org/3/whatsnew/2.7.html#other-language-changes
回答 3
这取决于您是否要使用文字进行比较或赋值。
如果要将现有集设为空,则可以使用该.clear()
方法,尤其是在要避免创建新对象的情况下。如果要进行比较,请使用set()
或检查长度是否为0。
例:
#create a new set
a=set([1,2,3,'foo','bar'])
#or, using a literal:
a={1,2,3,'foo','bar'}
#create an empty set
a=set()
#or, use the clear method
a.clear()
#comparison to a new blank set
if a==set():
#do something
#length-checking comparison
if len(a)==0:
#do something
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, use set()
or check if the length is 0.
example:
#create a new set
a=set([1,2,3,'foo','bar'])
#or, using a literal:
a={1,2,3,'foo','bar'}
#create an empty set
a=set()
#or, use the clear method
a.clear()
#comparison to a new blank set
if a==set():
#do something
#length-checking comparison
if len(a)==0:
#do something
回答 4
更加疯狂的想法是:在Python 3接受Unicode标识符的情况下,您可以声明一个变量ϕ = frozenset()
(ϕ为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.
回答 5
是。适用于非空dict / set的相同表示法适用于空dict / set。
注意非空dict
和set
文字之间的区别:
{1: 'a', 2: 'b', 3: 'c'}
-一个数字键-值对的内部使得dict
{'aaa', 'bbb', 'ccc'}
-一个元组值的内部使一个set
所以:
{}
==零个键值对==空dict
{*()}
==空值元组==空set
但是事实是您可以做到,但这并不意味着您应该这样做。除非您有很强的理由,否则最好显式构造一个空集,例如:
a = set()
注意:正如评论中注意到的那样,{()}
它不是一个空集合。这是一个包含1个元素的集合:空元组。
Yes. The same notation that works for non-empty dict/set works for empty ones.
Notice the difference between non-empty dict
and set
literals:
{1: 'a', 2: 'b', 3: 'c'}
— a number of key-value pairs inside makes a dict
{'aaa', 'bbb', 'ccc'}
— a tuple of values inside makes a set
So:
{}
== zero number of key-value pairs == empty dict
{*()}
== empty tuple of values == empty set
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:
a = set()
NB: As ctrueden noticed in comments, {()}
is not an empty set. It’s a set with 1 element: empty tuple.
回答 6
有几种方法可以在Python中创建空Set:
- 使用 set()方法
这是python中的内置方法,可在该变量中创建Empty set。
- 使用clear()方法(创造性的工程师技术LOL)
请参见以下示例:
sets = {“ Hi”,“ How”,“ are”,“ You”,“ All”}
类型(集合)(此行输出:set)
集合.clear()
print(sets)(此行输出:{})
type(sets)(此行输出:set)
因此,这是创建空Set的2种方法。
There are few ways to create empty Set in Python :
- Using set() method
This is the built-in method in python that creates Empty set in that variable.
- Using clear() method (creative Engineer Technique LOL)
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.