大括号的含义是什么?

发布于 2025-01-03 21:17:31 字数 410 浏览 2 评论 0原文

刚刚开始了解Python。我已阅读此问题及其答复:

我真的不能在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痕至 2025-01-10 21:17:31

Python 中使用“大括号”来定义字典。字典是一种将一个值映射到另一个值的数据结构 - 有点像英语字典如何将单词映射到其定义。

Python:

dict = {
    "a" : "Apple",
    "b" : "Banana",
}

它们也用于格式化字符串,而不是使用 % 的旧 C 风格,例如:

ds = ['a', 'b', 'c', 'd']
x = ['has_{} 1'.format(d) for d in ds]

print x

['has_a 1', 'has_b 1', 'has_c 1', 'has_d 1']

它们不用于表示代码块,因为它们在许多“类 C”语言中。

C:

if (condition) {
    // do this
}

更新:除了 Python 的 dict 数据类型之外,Python 还具有(自 Python 2.7 起)set 也是如此,它也使用大括号,声明如下:

my_set = {1, 2, 3, 4}

"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:

dict = {
    "a" : "Apple",
    "b" : "Banana",
}

They are also used to format strings, instead of the old C style using %, like:

ds = ['a', 'b', 'c', 'd']
x = ['has_{} 1'.format(d) for d in ds]

print x

['has_a 1', 'has_b 1', 'has_c 1', 'has_d 1']

They are not used to denote code blocks as they are in many "C-like" languages.

C:

if (condition) {
    // do this
}

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:

my_set = {1, 2, 3, 4}
月亮坠入山谷 2025-01-10 21:17:31

在Python中,花括号用于定义字典。

a={'one':1, 'two':2, 'three':3}
a['one']=1
a['three']=3

在其他语言中,{ } 用作流程控制的一部分。然而,Python 使用缩进作为其流程控制,因为它专注于可读代码。

for entry in entries:
     code....

Python 中的大括号有一个小彩蛋。尝试在 Python Shell 上运行它并享受吧。

from __future__ import braces

In Python, curly braces are used to define a dictionary.

a={'one':1, 'two':2, 'three':3}
a['one']=1
a['three']=3

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.

for entry in entries:
     code....

There's a little easter egg in Python when it comes to braces. Try running this on the Python Shell and enjoy.

from __future__ import braces
可爱暴击 2025-01-10 21:17:31

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.

云淡月浅 2025-01-10 21:17:31

Python 中的字典是存储键值对的数据结构。您可以像关联数组一样使用它们。声明字典时使用大括号:

d = {'One': 1, 'Two' : 2, 'Three' : 3 }
print d['Two'] # prints "2"

大括号不用于表示 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:

d = {'One': 1, 'Two' : 2, 'Three' : 3 }
print d['Two'] # prints "2"

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

浅沫记忆 2025-01-10 21:17:31

字典就像一个数组,可以通过键(例如字符串,...)访问,而不仅仅是简单的顺序数字。它包含键/值对,您可以使用键查找值,就像使用电话簿一样:键=名称,数字=值。

要定义这样的字典,您可以使用大括号来使用此语法,另请参阅: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文