简体   繁体   English

Python列表语法说明

[英]Python list syntax explanation

I've noticed that when I'm using python, I'll occasionally make a typographical error and have a definition that looks something like 我注意到,当我使用python时,我偶尔会出现一个打字错误并且有一个看起来类似的定义

L = [1,2,3,]

My question is, why doesn't this cause an error? 我的问题是,为什么这不会导致错误?

It doesn't cause an error because it is an intentional feature that trailing commas are allowed for lists and tuples. 它不会导致错误,因为它是一个故意的功能,允许列表和元组使用尾随逗号。

This is especially important for tuples, because otherwise it would be difficult to define a single element tuple: 这对于元组尤其重要,否则很难定义单个元素元组:

>>> (100,)   # this is a tuple because of the trailing comma
(100,)
>>> (100)    # this is just the value 100
100

It can also make it easier to reorder or add elements to long lists. 它还可以更容易地重新排序或添加元素到长列表。

From the Python docs: 从Python文档:

The trailing comma is required only to create a single tuple (aka a singleton); 尾随逗号只需要创建一个元组(也就是单个元素); it is optional in all other cases. 在所有其他情况下它是可选的。 A single expression without a trailing comma doesn't create a tuple, but rather yields the value of that expression. 没有尾随逗号的单个表达式不会创建元组,而是生成该表达式的值。 (To create an empty tuple, use an empty pair of parentheses: ().) (要创建一个空元组,请使用一对空括号:()。)

My question is, why doesn't this cause an error? 我的问题是,为什么这不会导致错误?

The trailing comma is ignored because it can be convenient: 尾随逗号被忽略,因为它很方便:

funcs = [ run,
          jump,
          # laugh
        ]

You can read more about in the official documentation: 您可以在官方文档中阅读更多信息:

Why does Python allow commas at the end of lists and tuples? 为什么Python在列表和元组的末尾允许使用逗号?

Python lets you add a trailing comma at the end of lists , tuples , and dictionaries : Python允许您在列表元组字典的末尾添加一个尾随逗号:

[1, 2, 3,]
('a', 'b', 'c',)
d = {
    "A": [1, 5],
    "B": [6, 7],  # last trailing comma is optional but good style
}

There are several reasons to allow this. 有几个理由允许这样做。

When you have a literal value for a list, tuple, or dictionary spread across multiple lines, it's easier to add more elements because you don't have to remember to add a comma to the previous line. 如果列表,元组或字典的文字值分布在多行中,则更容易添加更多元素,因为您不必记住在上一行中添加逗号。 The lines can also be sorted in your editor without creating a syntax error. 这些行也可以在编辑器中排序,而不会产生语法错误。

Accidentally omitting the comma can lead to errors that are hard to diagnose. 意外省略逗号会导致难以诊断的错误。 For example: 例如:

x = [
  "fee",
  "fie"
  "foo",
  "fum"
]

This list looks like it has four elements, but it actually contains three: "fee" , "fiefoo" and "fum" . 这个列表看起来有四个元素,但它实际上包含三个: "fee""fiefoo""fum" Always adding the comma avoids this source of error. 始终添加逗号可避免此错误来源。

Allowing the trailing comma may also make programmatic code generation easier. 允许尾随逗号也可以使编程代码生成更容易。

Do this 做这个

>>> l = 1,2,3,
>>> l
(1, 2, 3)

The () 's are optional. ()是可选的。 The , , means that you're creating a sequence. ,意味着你正在创建一个序列。

Observe this 观察这一点

>>> l = 1,
>>> l
(1,)
>>> l = 1
>>> l
1

Again. 再次。 The , means it's a sequence. ,意味着它是一个序列。 The () are optional. ()是可选的。

It's not wrong to think of 想到并没有错

[ 1, 2, 3, ]

as a tuple 1, 2, 3, inside a list constructor [ ] . 作为列表构造函数[ ]内的元组1, 2, 3, A list is created from the underlying tuple. 从基础元组创建列表。

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

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