使用卷曲大括号进行初始化(Use curly braces to initialize a Set in Python)

编程入门 行业动态 更新时间:2024-10-19 11:36:06
使用卷曲大括号进行初始化(Use curly braces to initialize a Set in Python)

我正在学习python,我有一个关于初始化集的新手问题。 通过测试,我发现一个集合可以像这样初始化:

my_set = {'foo', 'bar', 'baz'}

这样做有什么不利之处,而不是标准的方式:

my_set = set(['foo', 'bar', 'baz'])

还是只是一个风格的问题?

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:

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?

最满意答案

set literal语法有两个明显的问题:

my_set = {'foo', 'bar', 'baz'}

Python 2.7之前不可用

没有办法使用该语法来表达一个空集(使用{}创建一个空的dict)

那些对你来说可能或可能不重要。

文档的这一部分概述了这种语法。

There are two obvious issues with the set literal syntax:

my_set = {'foo', 'bar', 'baz'}

It's not available before 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.

更多推荐

本文发布于:2023-08-05 05:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428104.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:卷曲   括号   初始化   curly   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!