简体   繁体   English

在什么版本的Python中设置了初始化语法

[英]In what version of Python was set initialisation syntax added

I only just noticed this feature today! 我今天才注意到这个功能!

s={1,2,3} #Set initialisation
t={x for x in s if x!=3} #Set comprehension
t=={1,2}

What version is it in? 它是什么版本的? I also noticed that it has set comprehension. 我也注意到它已经设定了理解力。 Was this added in the same version? 这是在同一版本中添加的吗?

Resources 资源

The sets module was added in Python 2.3, but the built-in set type was added to the language in 2.4, with essentially the same interface. sets模块是在Python 2.3中添加的,但是内置的set类型被添加到2.4中的语言中,基本上具有相同的接口。 (As of 2.6, the sets module has been deprecated.) (截至2.6,不推荐使用sets模块。)

So you can use sets as far back as 2.3, as long as you 所以你可以使用早在2.3的套装,只要你

import sets

But you will get a DeprecationWarning if you try that import in 2.6 但是如果你在2.6中尝试导入,你将得到一个DeprecationWarning

Set comprehensions, and the set literal syntax -- that is, being able to say 设置理解和设置的文字语法 - 也就是说,能够说

a = { 1, 2, 3 }

are new in Python 3.0. 是Python 3.0中的新功能。 To be very specific, both set literals and set comprehensions were present in Python 3.0a1, the first public release of Python 3.0, from 2007. Python 3 release notes 非常具体,Python 3.0a1(Python 3.0的第一个公开发行版)从2007年开始提供了集合文字和集合理解.Python 3发行说明

The comprehensions and literals were later implemented in 2.7. 后来在2.7中实现了理解和文字。 3.x Python features incorporated into 2.7 3.x Python功能合并到2.7中

Well, testing it: 好吧,测试一下:

>>> s = {1, 2, 3}
  File "<stdin>", line 1
    s = {1, 2, 3}
          ^
SyntaxError: invalid syntax

I'm running 2.5, so I would assume that this syntax was added sometime in 2.6 (Update: actually added in 3.0, but Ian beat me). 我正在运行2.5,所以我会假设这个语法在2.6中添加了一段时间(更新:实际上在3.0中添加了,但Ian打败了我)。 I should probably be upgrading sometime soon. 我应该很快升级一段时间。 I'm glad they added a syntax for it - I'm rather tired of set([1, 2, 3]) . 我很高兴他们为它添加了一个语法 - 我厌倦了set([1, 2, 3])

Set comprehensions have probably been around since sets were first created. 自从首次创建集合以来,可能已经存在集合理解。 The Python documentation site isn't very clear, but I wouldn't imagine sets would be too useful without iterators. Python文档站点不是很清楚,但我不认为没有迭代器,集合会太有用。

The set literal and set and dict comprehension syntaxes were backported to 2.x trunk, about 2-3 days ago. 大约2-3天前,set literal和set和dict理解语法被移植到2.x trunk。 So I guess this feature should be available from python 2.7. 所以我想这个功能应该可以从python 2.7获得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM