【问题标题】:Python : How and why is this syntax working ? {1,2,3,4}Python:这种语法如何以及为什么起作用? {1,2,3,4}
【发布时间】:2012-09-28 13:06:18
【问题描述】:

众所周知,

{} # Represents a dict

{'one': 1} # Again a dict

这是一个集合的方式和原因:

{'one', 'two', 'three', 'four'} # I thought it should give syntax error

但它给出了:

set(['one', 'two', 'three', 'four']) # Strange ?? [ Should this happen ? ]

您能否提供一些讨论相同问题的官方文档的链接?

【问题讨论】:

  • 亲爱的投反对票的人,投反对票没有冒犯。只需添加评论,这有什么遗漏/错误?
  • 任何人都可以知道python中的任何其他有线/冲突语法吗?

标签: python list syntax dictionary set


【解决方案1】:

Set literals 是一个 3.x 功能,已被反向移植到 2.7。

这是一个有用的功能 - 请注意,集合推导也是允许的。

【讨论】:

  • (在 Python 3 中,集合文字符号也用于集合的 repr(),与 @YugalJindle 在 Python 2.7 中看到的相反)
  • 嗯,这个理解功能很酷。感谢分享。
猜你喜欢
  • 1970-01-01
  • 2012-12-30
  • 2019-09-30
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多