大括号的含义是什么?
刚刚开始了解Python。我已阅读此问题及其答复:
而且我仍然无法理解大括号是如何工作的,尤其是像 Simple Programs:
http://wiki.python.org/moin/SimplePrograms
到处都使用大括号。我理解方括号和常规弯括号,但我不知道“定义字典”是什么意思或者它们应该代表什么。
Just starting to figure Python out. I've read this question and its responses:
Is it true that I can't use curly braces in Python?
and I still can't fathom how curly braces work, especially since pages like Simple Programs:
http://wiki.python.org/moin/SimplePrograms
use curly braces all over the place. I understand square brackets and regular curved parentheses, but I don't know what's meant by "defining dictionaries" or what they're supposed to represent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Python 中使用“大括号”来定义字典。字典是一种将一个值映射到另一个值的数据结构 - 有点像英语字典如何将单词映射到其定义。
Python:
它们也用于格式化字符串,而不是使用 % 的旧 C 风格,例如:
它们不用于表示代码块,因为它们在许多“类 C”语言中。
C:
更新:除了 Python 的
dict
数据类型之外,Python 还具有(自 Python 2.7 起)set 也是如此,它也使用大括号,声明如下:"Curly Braces" are used in Python to define a dictionary. A dictionary is a data structure that maps one value to another - kind of like how an English dictionary maps a word to its definition.
Python:
They are also used to format strings, instead of the old C style using %, like:
They are not used to denote code blocks as they are in many "C-like" languages.
C:
Update: In addition to Python's
dict
data types Python has (since Python 2.7) set as well, which uses curly braces too and are declared as follows:在Python中,花括号用于定义字典。
在其他语言中,{ } 用作流程控制的一部分。然而,Python 使用缩进作为其流程控制,因为它专注于可读代码。
Python 中的大括号有一个小彩蛋。尝试在 Python Shell 上运行它并享受吧。
In Python, curly braces are used to define a dictionary.
In other languages, { } are used as part of the flow control. Python however used indentation as its flow control because of its focus on readable code.
There's a little easter egg in Python when it comes to braces. Try running this on the Python Shell and enjoy.
在
C
等语言中,大括号 ({}
) 用于创建流程控制中使用的程序块。在Python中,花括号用于定义称为字典(键/值映射)的数据结构,而空格缩进用于定义程序块。In languages like
C
curly braces ({}
) are used to create program blocks used in flow control. In Python, curly braces are used to define a data structure called a dictionary (a key/value mapping), while white space indentation is used to define program blocks.Python 中的字典是存储键值对的数据结构。您可以像关联数组一样使用它们。声明字典时使用大括号:
大括号不用于表示 Python 中的控制级别。相反,Python 使用缩进来实现此目的。
我认为你确实需要一些好的资源来学习 Python。请参阅https://stackoverflow.com/q/175001/10077
Dictionaries in Python are data structures that store key-value pairs. You can use them like associative arrays. Curly braces are used when declaring dictionaries:
Curly braces are not used to denote control levels in Python. Instead, Python uses indentation for this purpose.
I think you really need some good resources for learning Python in general. See https://stackoverflow.com/q/175001/10077
字典就像一个数组,可以通过键(例如字符串,...)访问,而不仅仅是简单的顺序数字。它包含键/值对,您可以使用键查找值,就像使用电话簿一样:键=名称,数字=值。
要定义这样的字典,您可以使用大括号来使用此语法,另请参阅:http://wiki.python。 org/moin/SimplePrograms
A dictionary is something like an array that's accessed by keys (e.g. strings,...) rather than just plain sequential numbers. It contains key/value pairs, you can look up values using a key like using a phone book: key=name, number=value.
For defining such a dictionary, you use this syntax using curly braces, see also: http://wiki.python.org/moin/SimplePrograms